preg_match for information in parentheses

北城余情 提交于 2019-12-12 01:22:27

问题


(ACCTG) Accounting

The text above I trying to get the information in the parentheses how would I do that in php this is what I have so far.

$regex = '#\((([^()]+|(?R))*)\)#';
if (preg_match_all($regex, $string ,$matches)) {
    echo implode(' ', $matches[1]);
} else {
    //no parenthesis
    echo $string;
}

回答1:


I do converting special characters to hex for easy use in my regex's

<?
$input = 'abc ("an example")';
if(preg_match("/\x28([^\x29]+)\x29/", $input, $matched)) { 
     //...         
    print_r($matched); 
} else { 
   //do something.. 
}
?>



回答2:


Wouldn't this be sufficient:

\(([^\)]*)\).*


来源:https://stackoverflow.com/questions/6378229/preg-match-for-information-in-parentheses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!