views:

59

answers:

2

Hello,

Im trying to find a regular expression that matches this kind of URL:

http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html

and dont match this:

http://sub.domain.com/selector/F/13/K/10546/sampletext/5987/K/sample/K/101/sample_text.html

only if the number of /K/ is minimum 1 and maximum 2 (something with a quantifier like {1,2})

Until this moment i have the following regexp:

http://sub\.domain\.com/selector/F/[0-9]{1,2}/[a-z0-9_-]+/

Now i would need a hand to add any kind of condition like:

Match this if in the text appears the /K/ from 1 to 2 times at most.

Thanks in advance.

Best Regards.

Josema

+1  A: 

Do you need to this all in one line?

The approach I would take is to do a regex for /K/ and then count the number of matches I got.

I think Boost is a C++ library right? In C# I would do it like this:

string url = "http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html";
if (Regex.Matches(url, "/K/").Count <= 2)
{
    // good url found
}

UPDATE

This regex would match everything up to the first two K's and then only allow the url filename.html after that:

^http://sub.domain.com/selector/F/[\d]+/[a-zA-Z]+/[\d]+/[a-zA-Z]+/[\d]+/K/[a-zA-Z_]+\.html$
rtpHarry
+1  A: 

This RE will match anything after the/F/[0-9]{1,2} that has 1 or 2 /K/, it could also match http://sub.domain.com/selector/F/13/K/100546/stuff/21515/stuff/sampletext/654654/K/stuff/sampletext_sampletext.html :

^http://sub\.domain\.com/selector/F/[0-9]{1,2}(?:/K(?=/)(?:(?!/K/)/[a-z0-9_.-]+)*){1,2}$
KArthur