了解preg_match公式

时间:2013-11-14 13:56:54

标签: php regex preg-match

我总是把我的东西用在跟随线上,但我真的无法理解。

if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Desktop/test/b '$test'"),$matches))
                {
                        $x = (int) $matches[1][0]; //optionally cast to int
                        $y = (int) $matches[1][1];
                        $pcount= round((100*$x)/($x+$y),2);
                        $ncount= round((100*$y)/($x+$y),2);
                }

b是可执行文件,其结果类似于x=10y=20

有人可以在if()

内解释我的任何内容

2 个答案:

答案 0 :(得分:2)

这:/[^=]*=([^;@]*)/将所有...=...内容收集到$ matches数组中。

  • [^=]表示除=
  • 之外的任何字符
  • [^;@]表示除了以外的任何字符;和@
  • ()表示将其明确收集到$ matches

$ pcount / $ ncount从显示其比率的值中获得百分比。

答案 1 :(得分:2)

模式细节:

[^=]*       # is a negated character class that means "all characters except ="
            # * is quantifier that means zero or more times
            # note that it seems more logical to replace it with + 
            # that means 1 or more times

=           # literal =

(           # open the capturing group 1
  [^;@]*    # all characters except ";" and "@", zero or more times
            # (same notice) 
)           # close the capturing group 1