Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php正则表达式在字符串中查找符号_Php_Regex - Fatal编程技术网

php正则表达式在字符串中查找符号

php正则表达式在字符串中查找符号,php,regex,Php,Regex,怎么连在一起 -1 -2 -3 -4 得到 使用正则表达式和函数 preg_match_all 感谢您的帮助!谢谢 使用以下命令: /(([-]?\d+))/gmi 结果是: MATCH 1 1. [1-3] `-1` MATCH 2 1. [8-10] `-2` MATCH 3 1. [15-17] `-3` MATCH 4 1. [22-24] `-4` 看 在PHP使用中: $string = '(-1) * (-2) - (-3) * (-4)'; $reg

怎么连在一起

-1 -2 -3 -4
得到

使用正则表达式和函数

preg_match_all
感谢您的帮助!谢谢

使用以下命令:

/(([-]?\d+))/gmi
结果是:

MATCH 1
1.  [1-3]   `-1`
MATCH 2
1.  [8-10]  `-2`
MATCH 3
1.  [15-17] `-3`
MATCH 4
1.  [22-24] `-4`

在PHP使用中:

  $string = '(-1) * (-2) - (-3) * (-4)';
  $regex = '/(([-]?\d+))/i';

  preg_match_all($regex, $string, $matches);

  print_r($matches[1]);
结果:

Array ( [0] => -1 [1] => -2 [2] => -3 [3] => -4 )
只需要使用(-1)*(-2)-(3)*(-4),而不需要使用(-1)*(-2)+(-3)*(-4)或其他
Array ( [0] => -1 [1] => -2 [2] => -3 [3] => -4 )