正则表达式:获取括号之外的内容
我正在使用 PHP,并且有如下文本:
first [abc] middle [xyz] last
我需要获取括号内和外的内容。在 StackOverflow 中搜索时,我发现了一种获取内部内容的模式:
preg_match_all('/\[.*?\]/', $m, $s)
现在我想知道获取外部内容的模式。
问候!
I'm using PHP and I have text like:
first [abc] middle [xyz] last
I need to get what's inside and outside of the brackets. Searching in StackOverflow I found a pattern to get what's inside:
preg_match_all('/\[.*?\]/', $m, $s)
Now I'd like to know the pattern to get what's outside.
Regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
preg_split
来实现此目的:输出:
这允许输出中存在一些周围的空格。如果您不需要它们,可以使用:
preg_split
根据模式拆分字符串。这里的模式是[
后跟任何内容,然后是]
。匹配任何内容的正则表达式是.*
。另外[
和]
是用于 char 类的正则表达式元字符。由于我们想要从字面上匹配它们,因此我们需要对它们进行转义以获得\[.*\]
。.*
默认是贪婪的,会尝试尽可能多的匹配。在这种情况下,它将匹配abc] 中间的 [xyz
。为了避免这种情况,我们通过附加?
来使其不贪婪,以给出\[.*?\]
。由于我们对这里任何内容的定义实际上意味着]
之外的任何内容,我们也可以使用\[[^]]*?\]
编辑:
如果如果您想要提取
[]
内部和外部的单词,可以使用:将字符串拆分为
[
或]
You can use
preg_split
for this as:Output:
This allows some surrounding spaces in the output. If you don't want them you can use:
preg_split
splits the string based on a pattern. The pattern here is[
followed by anything followed by]
. The regex to match anything is.*
. Also[
and]
are regex meta char used for char class. Since we want to match them literally we need to escape them to get\[.*\]
..*
is by default greedy and will try to match as much as possible. In this case it will matchabc] middle [xyz
. To avoid this we make it non greedy by appending it with a?
to give\[.*?\]
. Since our def of anything here actually means anything other than]
we can also use\[[^]]*?\]
EDIT:
If you want to extract words that are both inside and outside the
[]
, you can use:which split the string on a
[
or a]
或不太冗长
or less verbose
使用 preg_split 而不是 preg_match。
结果:
ideone
Use preg_split instead of preg_match.
Result:
ideone
正如每个人都说你应该使用 preg_split,但只有一个人用满足你需求的表达方式回复,
我认为这有点复杂- 不复杂,有点冗长,但他已经更新了他的回答反驳了这一点。这句话是大多数回复都说的。
但这只会打印出来,
并且您声明您想要大括号内部和外部的内容,sio 更新将是:
这给您:
但正如您所看到的,它也捕获了空白,所以让我们更进一步并摆脱这些:
这会给你一个想要的结果:
我认为这就是你正在寻找的表达。
这是上述正则表达式的实时演示
As every one says that you should use preg_split, but only one person replied with an expression that meets your needs,
and i think that is a little complex- not complex, a little to verbose but he has updated his answer to counter that.This expression is what most of the replies have stated.
But that only prints out
and you stated you wanted whats inside and outside the braces, sio an update would be:
This gives you:
but as you can see that its capturing white spaces as well, so lets go a step further and get rid of those:
This will give you a desired result:
This i think is the expression your looking for.
Here is a LIVE Demonstration of the above Regex