php 括号之间的php preg_match

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7729836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:15:21  来源:igfitidea点击:

php preg_match between bracket

phpstringpreg-match

提问by apis17

i want to use preg_match to find text between bracket for example: $varx = "(xx)";

我想使用 preg_match 在括号之间查找文本,例如: $varx = "(xx)";

final output will be $match = 'xx';

最终输出将是 $match = 'xx';

another example $varx = "bla bla (yy) bla bla";

另一个例子 $varx = "bla bla (yy) bla bla";

final output will be something like this $match = 'yy';

最终输出将是这样的 $match = 'yy';

in other words it strips out bracket. i'm still confusing with regular expression but found that sometimes preg match is more simple solution. searching other example but not meet my need.

换句话说,它去掉了括号。我仍然对正则表达式感到困惑,但发现有时预匹配是更简单的解决方案。搜索其他示例但不满足我的需要。

回答by Biotox

Try something like this:

尝试这样的事情:

preg_match('/(?<=\()(.+)(?=\))/is', $subject, $match);

Now it will capture newlines.

现在它将捕获换行符。

回答by CD001

Bear in mind that brackets are special characters in RegExps so you'll need to escape them with a backslash - you've also not made it clear as to what possible range of characters can appear between ( ... )or whether there can be multiple instances of ( ... ).

请记住,括号是 RegExps 中的特殊字符,因此您需要使用反斜杠将它们转义 - 您也没有明确表示之间可以出现哪些可能的字符范围( ... )或是否可以有多个( ... ).

So, your best bet might be a RegExp like: /\(([^\)]*)\)/which will match many occurrences of ( ... )containing any(or no) characters between the parentheses.

因此,您最好的选择可能是 RegExp 之类的:/\(([^\)]*)\)/它将匹配括号之间( ... )包含任何(或不包含)字符的多次出现。

Try preg_match('/\(([^\)]*)\)/', $sString, $aMatches)

尝试 preg_match('/\(([^\)]*)\)/', $sString, $aMatches)

EDIT: (example)

编辑:(示例)

<?php
$sString = "This (and) that";
preg_match_all('/\(([^\)]*)\)/', $sString, $aMatches);
echo $aMatches[1][0];
echo "\n\n\n";
print_r($aMatches);
?>

Which results in:

结果是:

and


Array
(
    [0] => Array
        (
            [0] => (and)
        )

    [1] => Array
        (
            [0] => and
        )

)

So the string "and" is stored in $aMatches[1][0] :)

所以字符串“and”存储在 $aMatches[1][0] :)

回答by Herbert

preg_match('/\((.*?)\)/i', $varx, $match);

preg_match('/\((.*?)\)/i', $varx, $match);

Adding the smodifier allows line-breaks between parentheses. For example:

添加s修饰符允许在括号之间换行。例如:

bla bla (y
y) bla bla

bla bla (y
y) bla bla

preg_match('/\((.*?)\)/si', $varx, $match);

preg_match('/\((.*?)\)/si', $varx, $match);

A better expression can be constructed if the content between paraentheses has a regular pattern. For example, if it were always double letters like xx or yy, the following expression would be a better match.

如果括号之间的内容具有规则模式,则可以构建更好的表达式。例如,如果它总是像 xx 或 yy 这样的双字母,则下面的表达式将是更好的匹配。

/\(([a-zA-Z]{2})\)/i

/\(([a-zA-Z]{2})\)/i

Also, if you want to capture allmatches in $varx, use preg_match_all(). For example:

此外,如果您想捕获 中的所有匹配项$varx,请使用preg_match_all(). 例如:

this (and) that (or) the other

this (and) that (or) the other

preg_match_all()will capture andand or

preg_match_all()将捕获andor

To test it use something like:

要测试它,请使用以下内容:

<?php
$varx = "this (and) that (or) the other";
preg_match_all('/\((.*?)\)/si', $varx, $matches);
print_r($matches);
?>

This will show where the matches are in the $matchesarray.

这将显示匹配项在$matches数组中的位置。

回答by Dhiraj

I hope this works

我希望这有效

preg_match_all('#\(((?>[^()]+)|(?R))*\)#x');

I'm sorry for the incomplete answer..

我很抱歉不完整的答案..

$preg_match = '#\(((?>[^()]+)|(?R))*\)#x';

$data = 'bla bla (yy) bla bla';

if(preg_match_all($preg_match, $data, $match)) {
    $match = $match[0];
} else {
    $match = array();
}
$preg_match = '#\(((?>[^()]+)|(?R))*\)#x';

$data = 'bla bla (yy) bla bla';

if(preg_match_all($preg_match, $data, $match)) {
    $match = $match[0];
} else {
    $match = array();
}

Hope is makes sense now

希望现在是有道理的

回答by Taylor Hawkes

This seems cleaner.

这看起来更干净。

$pattern = '/\((.+?)\)/is';

回答by genesis

Do not use regex for this, use str_replace()

不要为此使用正则表达式,请使用 str_replace()

$string = str_replace(array('(', ')'), '', $your_string);