views:

28

answers:

2

I've got the these strings:

/search/keyword.html 

and

/search/keyword/1.html  # the 1 represents the current page

Now I want to write a regex that matches the keyword, and the page, if given.

$1 (the first match) should always be the keyword (keyword).

$2 should be the page, if there is one (1).

I tried this but it doesn't work correctly:

~/search/((.*)/(\d*)|(.*)).html~

$1 is not always the keyword and $2 not the page (if given).

+1  A: 

Try

~/search/([^/.]*)(?:/(\d+))?\.html~

Explanation:

/search/      # match /search/ literally
([^/.]*)      # match any number of characters except . or /, capture in backref 1
(?:           # if present...
 /(\d+)       # match a / and one or more digits, capture the latter in backref 2
)?            # ...end of optional group
\.html        # match .html literally
Tim Pietzcker
thanks a lot :)
ipsum
A: 

Depending on your regexp flavor:

,^/search/([^/]+)/(.*)\.[^.]+$,
icanhasserver