Regex help

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
 
W

Willem van Rumpt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regex in C# 4
regex multiplication problem 3
Regex with time 3
Regex help needed 1
RegEx Format Help 4
regex help 1
Regex - Matching URLS 2
more regex question how to avoid capturing leading empty lines 2

Top