简体   繁体   English

RegEx模式以匹配两个字符之间的字符串,但排除字符

[英]RegEx pattern to match a string between two characters, but exclude the characters

Greetings all, 大家问候,

I have yet another RegEx question. 我还有另一个RegEx问题。 I have done hours of searching but apparently I have missed the one key article I need. 我已经进行了数小时的搜索,但显然我错过了我需要的一篇关键文章。

I need to use preg_match() in PHP to match a string that is between parenthesis, but NOT have the parenthesis show up in the result. 我需要在PHP中使用preg_match()来匹配括号之间的字符串,但结果中没有括号。 I have seen examples of similar issues, but I believe my problem is different because it's actually parenthesis that I am dealing with, with is a meta character in RegEx. 我已经看到了类似问题的示例,但是我相信我的问题有所不同,因为它实际上是我要处理的括号,它是RegEx中的一个元字符。 Any merit to that? 有什么好处吗?

Anyways... 无论如何...

String is: 字符串是:

     "200 result=1 (SIP/100-00000033)"

Current code is: 当前代码是:

preg_match("/\((.*)\)/s", $res, $matches);

$matches[0] becomes: $ matches [0]变为:

     "(SIP/100-00000033)"

What I WANT is: 我想要的是:

     "SIP/100-00000033"

I apologize because I'm sure this is VERY simple but I'm just not grasping it. 我很抱歉,因为我敢肯定这很简单,但我没有把握。 Would anyone care to educate me? 有人愿意教育我吗?

Thank you in advance!! 先感谢您!!

Well, it all refers to the way you group items in the regular expression. 嗯,所有这些都指的是在正则表达式中对项目进行分组的方式。 Your solution is actually correct, you're just using the wrong index for matches. 您的解决方案实际上是正确的,只是您使用了错误的匹配索引。 Try: 尝试:

$matches[1]

If that somehow gives errors, post'em and we'll fix. 如果那以某种方式给您带来了错误,请在发布后再解决。

如果您确实希望完全匹配排除括号,则可以使用先行和后行断言

preg_match('/(?<=\().*(?=\))/s', $res, $matches);

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

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