Reputation: 381
Using ASP.NET, and trying to implement friendly url re-writing but I'm having a heck of a time working with the regular expression. Basically, I'm checking the url directly following the domain to see whether it is using the french-canadian culture, or whether it is a number - and not a number followed by characters. I need to catch anything that begins with 'fr-ca' OR a number, and both scenarios can have a trailing '/' (forward slash), but that's all...
fr-ca - GOOD
fr-ca/ - GOOD
fr-ca/625 - GOOD
fr-ca/gd - BAD
fr-ca43/ - BAD
1234 - GOOD
1234/ - GOOD
1234/g - GOOD
1234g - BAD
1g34 - BAD
This is what I've come up with : ^(fr-ca)?/?([0-9]+)? But it doesn't seem to be working the way I want.. so I started fresh and came up with (^fr-ca)|(^[0-9]), which still isn't working the way I want. Please...HELP!
Upvotes: 1
Views: 190
Reputation: 195059
no idea about Asp.net, but the regexp was tested with grep. you could try in your .net box:
kent$ cat a
fr-ca
fr-ca/
fr-ca/625
fr-ca/gd
fr-ca43/
1234
1234/
1234/g
1234g
1g34
updated
kent$ grep -P "(^fr-ca$|^\d+$|^(fr-ca|\d+)/(\w|\d*)$)" a
fr-ca
fr-ca/
fr-ca/625
1234
1234/
1234/g
--
well this may not be the best regex, but would be the most straightforward one.
it matchs string
^fr-ca$
or ^\d+$
or ^(fr-ca|\d+)/(\w|\d*)$
the above line can be broken down as well
^(fr-ca|\d+)/(\w|\d*)$ :
starting with fr-ca or \d+
then comes "/"
after "/" we expect \w or \d*
then $(end)
Upvotes: 2
Reputation: 92986
What about
^(fr-ca(?:\/\d*)?|[0-9]+(\/[a-zA-Z]*)?)$
See it here on Regexr, it matches all your good cases and not the bad ones.
Upvotes: 2
Reputation: 14279
^(\d+(\/\w*)?)$|^(fr-ca\/\d+)$|^(fr-ca\/?)$
This worked for me for all of your examples. I'm not sure your intention for using this regex so I don't know if it is capturing exactly what you want to capture.
Upvotes: 1
Reputation: 11922
Probably can try...
^(fr-ca|\d).*$
But of course this a regex to match the entire string (as it has the end-of-sting $
anchor). Are you wanting to pull out multiple matches?
In light of re-reading the post :)
^(fr-ca|\d+)(\/\d+|\/)?$
Upvotes: 1