简体   繁体   中英

Regex replace pattern - PHP

I need some help, I have a replacement pattern:

/(?<!\S)(\b|\(|\[)(?:\d+\s*x\s*)?\d+(?:\.\d+)?\s*litres($|\s|\.|;|,|\)|\])/is    

with function:

preg_replace('/(?<!\S)(\b|\(|\[)(?:\d+\s*x\s*)?\d+(?:\.\d+)?\s*litres($|\s|\.|;|,|\)|\])/is', '', $string);

which replace to '' - empty string and in 99% cases works great, however there is some which are not working as exprected.

So ie

Bottle (blue, 10 Litres) - will be replaced to Bottle (blue, instead of Bottle (blue,) .

it looks like the right boundry {in example above ) sign} is being treated as a part of the string to replace. Any ideas?

cheers

Seems that backreference works: ${2}

so ie

preg_replace('/(?<!\S)(\b|\(|\[)(?:\d+\s*x\s*)?\d+(?:\.\d+)?\s*litres($|\s|\.|;|,|\)|\])/is', '${2}', $string);

should do the trick.

What you reckon?

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