Regex help

  • Thread starter Thread starter Sems
  • Start date Start date
S

Sems

Hi

I am using a regular expression and want to change it a little bit but
am not sure how.

I have this regex...

@"^(?<page>sport/news/[^\s/]+)(?<articlePage>/\d+/\d+/\d+/\S+)$"

Which mateches...

http://mysite.com/sport/news/news-cat-1/2011/01/21/article-1

I also want it to match this...

http://mysite.com/sport/somethingelse/news-cat-1/2011/01/21/article-1

So I want to remove the 'news' in the regex to match any string. How
would I do this in this case? I tried this but it didn't work...

@"^(?<page>sport/[\S]/[^\s/]+)(?<articlePage>/\d+/\d+/\d+/\S+)$"

Thanks
 
http://mysite.com/sport/news/news-cat-1/2011/01/21/article-1

I also want it to match this...

http://mysite.com/sport/somethingelse/news-cat-1/2011/01/21/article-1

So I want to remove the 'news' in the regex to match any string. How
would I do this in this case? I tried this but it didn't work...

Assuming the format of the url is always like the examples given, you
could use this:

(?<page>sport)/(?<category>[^/]*)/(?<subcategory>[^/]*)/(?<date>\d{4}/\d{2}/\d{2})/(?<article>.*)

In your examples, this will extract the following groups:

_http://mysite.com/sport/news/news-cat-1/2011/01/21/article-1_

<page> = sport
<category>= news
<subcategory>= news-cat-1
<date>= 2011/01/21
<article>=article-1

_http://mysite.com/sport/somethingelse/news-cat-1/2011/01/21/article-1_

<page> = sport
<category>= somethingelse
<subcategory>= news-cat-1
<date>= 2011/01/21
<article>=article-1
 
Back
Top