简体   繁体   English

PHP(Regex)-将网址部分与括号匹配

[英]PHP (Regex) - Match url part with parenthesis

I am pretty new to regex (only really started looking seriously to it a few hours ago) and even if it is not going that bad... I am having a bad time matching parenthesis. 我对regex还是很陌生(只是几个小时前才真正开始认真看待它),即使它并没有那么糟糕...我在匹配括号的时候也很糟糕。

Here's what I have / want: 这是我想要的东西:

$p = "#(\/new_arrivals\/[a-zA-Z0-9_]+|\/\(offset\)\/([0-9][0-9]|[0-9][0-9][0-9]))$# i";
$s = "http://www.example.com/abc/cba/new_arrivals/(offset)/29";

I would like to match /(offset)/29 ... but I can't seem to be able to make it match the parenthesis even if I did try to escape them. 我想匹配/(offset)/29 ...,但是即使我确实尝试了转义,我似乎也无法使其与括号匹配。

Also.. the number 29... starts into 29 and goes up till 500. Is there any simpler way to make it match 2 or 3 numbers instead all of this ([0-9][0-9]|[0-9][0-9][0-9]) ? 还有..数字29 ...从29开始一直上升到500。有没有更简单的方法来匹配2或3个数字,而不是全部([0-9][0-9]|[0-9][0-9][0-9])

Use this regex: 使用此正则表达式:

\/\(offset\)\/\d{2,3}

Demo: http://regex101.com/r/tZ7zS4 演示: http//regex101.com/r/tZ7zS4

\\/\\(*[Az]+\\)*\\/[0-9]+$

matches: /(offset)/29 匹配:/(偏移)/ 29

\\/[Az]+\\/[0-9]+$

matches: /offset/29 匹配:/ offset / 29

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM