【发布时间】:2016-07-20 04:54:23
【问题描述】:
我遇到了一些与正则表达式和模式相关的问题。 我的输入必须是 A 或 B 格式或 A 和 B 组合多次。
说明一下
一个模式是 x(y)
B 模式是 x(y,z)
A,B 表示 x(y),x(y,z)
组合可以像但不限于
A,A
A,A,A
A,B,A
B,B
乙,乙,乙
等等,每个组合没有特定的数字。 是否有任何可用的方法来验证组合中的 A 和 B 的格式是否正确。
我认为我应该做的是在分隔符处拆分组合,然后通过验证我的 A 和 B 的函数传递每个数组元素。
$rule=$_POST['rule']; //e,g aaa(bbb,ccc) OR aaa(bbb)
$patternR1='/^\w+[\(]\w+[\)]$/';
$patternR2='/^\w+[\(]\w+[\,]\w+[\)]$/';
if (preg_match($patternR1, $rule ))
{
echo "Your entered rule is ".$rule." satisfying the correct format: x(y)";
}
else if(preg_match($patternR2, $rule))
{
echo "Your entered rule is ".$rule." satisfying the correct format: x(y,z)";
}
else
{
echo "Syntax error in the rule body ". $rule." has to be either in x(y) or x(y,z) format";
$ef=0;
}
另外请注意,A 和 B 之间有一个逗号分隔,模式 B 中有一个逗号,在上下文中具有不同的含义。
【问题讨论】: