Regex to match comma not between grouping symbols

对着背影说爱祢 提交于 2019-12-30 06:48:50

问题


I need a regular expression that will match a comma that is NOT between either a '[' and ']' or '(' and ')' or '{' and '}'. Other grouping symbols do not matter. I have tried to figure it out but I cannot come up with anything that accomplishes this.

The regex is to be used with the PHP preg_split function to split a string on the matched commas.

An example string containing commas and grouping symbols:

<div>Hello<div>,@func[opt1,opt2],{,test},blahblah

The string should split up as follows:

1: '<div>Hello<div>'
2: '@func[opt1,opt2]'
3: '{,test}'
4: 'blahblah'

And I just thought of this, but at this point all grouping symbols are guaranteed to have matching symbols, incase that helps.

Any help would be GREATLY appriciated =)


回答1:


Actually it is not impossible to get this splitting done. Consider this code:

$str = '<div>Hello<div>,(foo,bar),@func[opt1,opt2],{,test},blahblah';
$arr = preg_split('~([^,]*(?:{[^}]*}|\([^)]*\)|\[[^]]*])[^,]*)+|,~', $str, -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
var_dump($arr);

OUTPUT:

array(5) {
  [0]=>
  string(15) "<div>Hello<div>"
  [1]=>
  string(9) "(foo,bar)"
  [2]=>
  string(16) "@func[opt1,opt2]"
  [3]=>
  string(7) "{,test}"
  [4]=>
  string(8) "blahblah"
}



回答2:


I don't think it can be done in a regular expression. The basic problem is that this requires variable length negative look-behinds (disallow any [({ that is not followed by a ])}), and that isn't a capability which RE currently has.



来源:https://stackoverflow.com/questions/6132911/regex-to-match-comma-not-between-grouping-symbols

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