如何让正则表达式忽略括号之间的所有内容? [英] How to let regex ignore everything between brackets?
本文介绍了如何让正则表达式忽略括号之间的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下字符串:
I have been driving to {Palm.!.Beach:100} and it . was . great!!
我使用以下正则表达式删除所有标点符号:
I use the following regex to delete all punctuation:
$string preg_replace('/[^a-zA-Z ]+/', '', $string);
输出:
I have been driving to PalmBeach and it was great!!
但我需要正则表达式始终忽略 { 和 } 之间的任何内容.所以期望的输出是:
But I need the regex to always ignore whatever is in between { and }. So the desired output would be:
I have been driving to {Palm.!.Beach:100} and it was great
如何让正则表达式忽略 { 和 } 之间的内容?
How can I let the regex ignore what is between { and }?
推荐答案
试试这个
[^a-zA-Z {}]+(?![^{]*})
表示匹配任何不包含在否定字符类中的东西,但前提是前面没有右括号而前面没有开括号,这是通过否定前瞻(?![^{]*})代码>.
Means match anything that is not included in the negated character class, but only if there is no closing bracket ahead without a opening before, this is done by the negative lookahead (?![^{]*})
.
$string preg_replace('/[^a-zA-Z {}]+(?![^{]*})/', '', $string);
这篇关于如何让正则表达式忽略括号之间的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文