开发者

problem with regex (find page if given)

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开发者_C百科 (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).


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


Depending on your regexp flavor:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜