简体   繁体   English

正则表达式替换模式-PHP

[英]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. 替换为''-空字符串,在99%的情况下效果很好,但是有一些不正确。

So ie 所以

Bottle (blue, 10 Litres) - will be replaced to Bottle (blue, instead of Bottle (blue,) . 瓶子(蓝色,10升)-将替换为瓶子(蓝色,而不是瓶子(蓝色))

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} 似乎反向引用有效: $ {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? 你认为呢?

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

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