Sanosay
Sanosay

Reputation: 578

Regex extract numbers from Url (except port number)

i have to extract id value of a product from url.

It is SEO Friendly (url routing).

Url can be

http://www.example.com/{param0}/{param1}/123/{param2}/{paramN}

Or

http://localhost:6847/{param0}/{param1}/123/{param2}/{paramN}

For the first url there is no problem. But for the second i want to extract ONLY the 123 or (ID) <-(It is an Integer).

I know that if i want to extract only numbers i can use

[0-9]+

but how can i tell regengine how to get all the numerical data from url except numbers that may have

:

before. i use :

((!:)[0-9]+) 

it is not correct. Every advice is wellcamed:)

Thank you.

Upvotes: 1

Views: 853

Answers (1)

user557597
user557597

Reputation:

There needs to be more info on what delimits the 123 in your example.

On its face, (?<!:)[0-9]+ will find the first clump of digits NOT preceded by ':'

Edit Probably for more accuracy, this (?<!:\d+)[0-9]+ would be better.
Note this is if .NET allows variable length look-behind (I think it does).

For fixed length look-behind (PCRE), something like this might work: (?<![:\d])[0-9]+

Edit2

@Sanosay- After thinking about .NET type lookbehinds, the above regex needs a slight change. It should be (?<!:\d*)[0-9]+ . Thats because in ':1234', 1 will satisfy the assertion.

Hope you figured this to be the case. I made a test case for the two regex's
@"(?<!:\d*)[0-9]+"
@"(?<![:\d])[0-9]+"
that satisfy the conditions.

The link to the ideone C# code is here: http://ideone.com/tLn2j

Upvotes: 1

Related Questions