结合 2 个正则表达式 [英] Combining 2 regex expressions

查看:127
本文介绍了结合 2 个正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试结合 2 个正则表达式.

I'm looking to try and combine 2 regex expressions.

$test = preg_replace('/\[.*?\]/', '', $test);   

我用它来删除方括号和方括号之间的单词..

I use this to remove any square brackets and words in between the brackets..

$test = preg_replace ('/[\[\]=]+/', '', $test);

我用它从我的字符串中去除任何 = [ ] 符号.

I use this to strip any = [ ] symbols from my string.

单独做这件事似乎没问题,但我想将它们结合起来.任何我能做到的想法..我尝试过的一切都破坏了两者..

doing this individually seems to be fine, but I'd like to combine them. Any ideas how I can do that.. everything I've tried has broken both..

谢谢

推荐答案

使用替代方案:

$test = preg_replace('/\[.*?\]|[\[\]=]/', '', $test);

这里的排序很重要,因此首先匹配的括号及其内容将被删除.只要没有嵌套括号,这应该可以正常工作.

The ordering is here important, so that at first matching brackets, and their content, are removed. This should work fine as long as there are no nested brackets.

所以,这个表达式将匹配 \[.*?\] OR [\[\]=]

So, this expression will match either \[.*?\] OR [\[\]=]

这篇关于结合 2 个正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆