使用正则表达式提取括号中的内容
我真的完全不懂正则表达式,这让我很头疼。
我有一些看起来像这样的文本
blah blah blah (here is the bit I'd like to extract)
...我不太明白如何使用 PHP 的 preg_split 或等效命令来提取它。
我该怎么做?哪里是了解 preg 工作原理的好地方?
I really don't understand regex at all, and it hurts my head.
I've a bit of text which looks like this
blah blah blah (here is the bit I'd like to extract)
...and I don't really understand how to extract this using PHP's preg_split, or equivalent, command.
How do I do this? And where's a good place to understand how preg works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西应该可以解决问题,以匹配
(
和)
之间的内容:你会得到:
基本上,我使用的模式搜索:
(
;但是 as ( 具有特殊含义,必须转义:\(
[^\)]+
([^\)]+)
$matches[1]
)
;在这里,它也是一个必须转义的特殊字符:\)
Something like this should do the trick, to match what is between
(
and)
:And you'd get :
Basically, the pattern I used searches for :
(
; but as ( has a special meaning, it has to be escaped :\(
[^\)]+
([^\)]+)
$matches[1]
)
; here, too, it's a special character that has to be escaped :\)