简体   繁体   中英

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.

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.

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]) ?

Use this regex:

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

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

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

matches: /(offset)/29

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

matches: /offset/29

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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