1

I have a string that contains a lot of information, including a total count and current count. I want to use regex and php to match either [001/100] or (001/100).

  • It can either be wrapped in parentheses or brackets.
  • It may also contain spaces inside the parentheses or brackets ( 001/100).
  • The delimiter can also be a : or |.

I want to ignore all spaces within the parentheses or brackets and the numbers can be any digets.

I plan to use preg_match_all. I am just struggling to figure out the regex.

preg_match_all('/[\(\[](\d+)[\:\|](\d+)[\)\]]/' $tmp, $matches);
4

2 回答 2

3

This should work for you but allows mixing parentheses and square brackets.

[([]\s*(\d+)\s*[/:|]\s*(\d+)\s*[)\]]
于 2012-12-19T19:54:42.390 回答
0
preg_match_all('@
   [([] #open paren or brace
   \s* #0 or more spaces
   (\d+) #capture the first number
   \s*
   [:/|] #delimeter
   \s*
   (\d+) #capture the second number
   \s*
   [)\]] #capture the closing paren or brace
@x', $string, $matches);
于 2012-12-19T19:59:20.273 回答