简体   繁体   中英

negative lookaround regex

i have the following string

.proxy.com  TRUE    /   FALSE   0   COOKIE%253BCartID%253B%252F%253Bwww.proxy.com   1914104745%253B

and the following regex expression

[a-zA-Z0-9\%]{14,15}

i want to only match 1914104745%253B but it is picking up COOKIE%253BID, i tried to do negative assertion like this

[a-zA-Z0-9\%]{14,15}[?!COOKIE]

but that does not work

can anyone help with this regex expression?

Why not just anchor to the end of the string with [a-zA-Z0-9%]{14,15}$

$str=".proxy.com  TRUE    /   FALSE   0   COOKIE%253BCartID%253B%252F%253Bwww.proxy.com   1914104745%253B";
preg_match('/[a-zA-Z0-9%]{14,15}$/',$str,$match); 
echo $match[0];

>>> 1914104745%253B

If the part of the string you want to match is strictly formatted you could use something such as \\d{10}%\\d{3}[AZ]$ and you can drop the $ if the match won't always be at the end of the string, either way only 1914104745%253B will be matched in your example.

Note: % doesn't needed escaping.

Try this

(?<=\s)[\w%]{14,15}(?:(?=\s)|$)

Demo

Lookaheads use round brackets, not square ones. But I don't think a look ahead on its own will help here:

(?!COOKIE)[a-zA-Z0-9\%]{14,15}

Would prevent it matching COOKIE%253BCart but then it will simply match OOKIE%253BCartI instead. Anchoring the match at the end of the line using a trailing $ is probably the simplest approach.

Are you simply trying to get the last string of non-whitespace characters on the line? Then use this

(\S+)$

You could also use preg_split to split on whitespace and taken the last element of the array it returns.

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