php regex方括号和逗号

时间:2021-04-25 21:41:10

I want to capture the text within the square brackets and commas in the string below.

我想在方括号内捕获文本,在下面的字符串中捕获逗号。

$_['text_1']      = 'text_2 %d to %d of %d (%d text)';

I have a regex for square brackets

我有方括号的正则表达式

preg_match_all("/\[[^\]]*\]/",$string,$matches); //all strings with brackets

but need two regex for

但是需要两个regex

  1. text_1
  2. text_1
  3. text_2 %d to %d of %d (%d text)
  4. text_2 %d %d (%d文本)

1 个解决方案

#1


2  

Your "/\[[^\]]*\]/" only matches substrings like [.[.[..].

”/ \[[^ \]]* \]/”只匹配子像[。[[…]。

You can easily get the values using

您可以轻松地使用这些值

(?|\['([^][]*)']|'([^'\\]*(?:\\.[^'\\]*)*)')

See regex demo, your values will be in Group 1.

参见regex demo,您的值将在组1中。

PHP demo:

PHP演示:

$re = "/(?|\\['([^][]*)']|'([^'\\\\]*(?:\\\\.[^'\\\\])*)')/"; 
$str = "\$_['text_1']      = 'text_2 %d to %d of %d (%d text)';"; 
preg_match_all($re, $str, $matches);
print_r($matches[1]);

The regex contains 2 alternatives with 2 capturing groups that have Index 1 since a branch reset grouping (?|..|..) is used. The 2 alternatives match:

regex包含两个可选方案,其中有两个捕获组,它们具有索引1,因为使用了分支重置分组(?|..|.. .. .. .. .. .. .. .. .. ..。2选择匹配:

  • \['([^][]*)'] - a literal [', followed with 0 or more characters other than ] and [ up to ']
  • \['([^][]*)”)——一种文字[',0或多个字符除了]和[到']
  • '([^'\\]*(?:\\.[^'\\]*)*)') - any substring inside single quotes that may contain any escaped sequences.
  • ”([^ *(?:\ \ ' \ \]。[^ \ \]*)*))——任何子字符串可能包含任何转义序列的单引号内。

Or a safer one that will also allow any [ or ] inside the first group (i.e. if you have $_['P[O]ST'] though I guess it is unlikely):

或者是一种更安全的方法,允许第一类人(也就是说,如果你有$_['P[O]ST]],尽管我认为不太可能):

(?|\['([^']*(?:'(?!])[^']*)*)'\]|'([^'\\]*(?:\\.[^'\\]*)*)')

See another demo

看到另一个演示

#1


2  

Your "/\[[^\]]*\]/" only matches substrings like [.[.[..].

”/ \[[^ \]]* \]/”只匹配子像[。[[…]。

You can easily get the values using

您可以轻松地使用这些值

(?|\['([^][]*)']|'([^'\\]*(?:\\.[^'\\]*)*)')

See regex demo, your values will be in Group 1.

参见regex demo,您的值将在组1中。

PHP demo:

PHP演示:

$re = "/(?|\\['([^][]*)']|'([^'\\\\]*(?:\\\\.[^'\\\\])*)')/"; 
$str = "\$_['text_1']      = 'text_2 %d to %d of %d (%d text)';"; 
preg_match_all($re, $str, $matches);
print_r($matches[1]);

The regex contains 2 alternatives with 2 capturing groups that have Index 1 since a branch reset grouping (?|..|..) is used. The 2 alternatives match:

regex包含两个可选方案,其中有两个捕获组,它们具有索引1,因为使用了分支重置分组(?|..|.. .. .. .. .. .. .. .. .. ..。2选择匹配:

  • \['([^][]*)'] - a literal [', followed with 0 or more characters other than ] and [ up to ']
  • \['([^][]*)”)——一种文字[',0或多个字符除了]和[到']
  • '([^'\\]*(?:\\.[^'\\]*)*)') - any substring inside single quotes that may contain any escaped sequences.
  • ”([^ *(?:\ \ ' \ \]。[^ \ \]*)*))——任何子字符串可能包含任何转义序列的单引号内。

Or a safer one that will also allow any [ or ] inside the first group (i.e. if you have $_['P[O]ST'] though I guess it is unlikely):

或者是一种更安全的方法,允许第一类人(也就是说,如果你有$_['P[O]ST]],尽管我认为不太可能):

(?|\['([^']*(?:'(?!])[^']*)*)'\]|'([^'\\]*(?:\\.[^'\\]*)*)')

See another demo

看到另一个演示