Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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
Regex 正则表达式帮助,如何使表达式的顺序无关紧要?_Regex - Fatal编程技术网

Regex 正则表达式帮助,如何使表达式的顺序无关紧要?

Regex 正则表达式帮助,如何使表达式的顺序无关紧要?,regex,Regex,我不知道如何获得传入字符串参数price、merchant、category的顺序对regex来说并不重要。我的正则表达式匹配字符串的各个部分,但不匹配整个字符串。我需要能够向其中添加\A\Z Pattern: (,?price:(;?(((\d+(\.\d+)?)|min)-((\d+(\.\d+)?)|max))|\d+)+){0,1}(,?merchant:\d+){0,1}(,?category:\d+){0,1} Sample Strings: price:1.00-max;3

我不知道如何获得传入字符串参数price、merchant、category的顺序对regex来说并不重要。我的正则表达式匹配字符串的各个部分,但不匹配整个字符串。我需要能够向其中添加\A\Z

Pattern: 
 (,?price:(;?(((\d+(\.\d+)?)|min)-((\d+(\.\d+)?)|max))|\d+)+){0,1}(,?merchant:\d+){0,1}(,?category:\d+){0,1}

Sample Strings:

price:1.00-max;3-12;23.34-12.19,category:3

merchant:25,price:1.00-max;3-12;23.34-12.19,category:3

price:1.00-max;3-12;23.34-12.19,category:3,merchant:25

category:3,price:1.00-max;3-12;23.34-12.19,merchant:25

注意:我将在我的所有组中添加?:在我让它工作后。

不要尝试用一个Cthulhugex来完成所有工作,怎么样

/price:([^,]*)/
/merchant:([^,]*)/
/category:([^,]*)/

您可能应该通过正常解析来解析这个字符串。在逗号处拆分,然后用冒号将每个部分拆分为两部分。如果希望单独检查每个输入,可以存储验证正则表达式


如果你通过正则表达式这样做,你可能最终不得不说出这个组合或这个组合或这个组合,这会带来很大的伤害。

你有三个选择:

你可以列举所有可能的订单。对于3个变量,有6种可能性。显然,这并不具有可伸缩性; 您可以接受可能的副本;或 您可以将字符串拆分,然后对其进行解析。 2的意思是:

/(\b(price|category|merchant)=(...).*?)*/
这里面临的真正问题是,您试图用正则表达式解析本质上是非正则语言的东西。正则表达式描述DFSM确定性有限状态机或DFA确定性有限自动机。正则语言没有状态的概念,因此表达式无法记住还有什么

要做到这一点,你必须添加一个通常以堆栈形式存在的内存,这会产生一个PDA下推自动机

当人们试图用正则表达式解析HTML时,遇到标签嵌套问题和类似的问题,这与他们面临的问题完全相同


基本上,您接受一些边缘条件,如重复值,用逗号分割字符串,然后进行解析,或者您只是使用了错误的工具来执行作业。

什么语言?每种编程语言都有自己的正则表达式风格。此外,了解该语言可能会使我们更容易说服您不要使用正则表达式;另外,您只是想验证输入,还是想从中提取信息?我注意到您的第一个字符串缺少一个商户条目;这真的有效吗?这会对我们提供的解决方案产生很大影响。@Alan,我只是想验证一下。谢谢你的完整解释。我将拆分字符串,然后分别验证它们。
$string=<<<EOF
price:1.00-max;3-12;23.34-12.19,category:3

merchant:25,price:1.00-max;3-12;23.34-12.19,category:3

price:1.00-max;3-12;23.34-12.19,category:3,merchant:25

category:3,price:1.00-max;3-12;23.34-12.19,merchant:25
EOF;

$s = preg_replace("/\n+/",",",$string);
$s = explode(",",$s);
print_r($s);
$ php test.php
Array
(
    [0] => price:1.00-max;3-12;23.34-12.19
    [1] => category:3
    [2] => merchant:25
    [3] => price:1.00-max;3-12;23.34-12.19
    [4] => category:3
    [5] => price:1.00-max;3-12;23.34-12.19
    [6] => category:3
    [7] => merchant:25
    [8] => category:3
    [9] => price:1.00-max;3-12;23.34-12.19
    [10] => merchant:25
)