PHP的preg_match噩梦
作者:互联网
我只是无法绕过正则表达式,感谢任何帮助!
我有很多字符串数据,它们可能包含也可能不包含字符串“ 1/10”或“ 2/10”或“ 2/18”等.基本上,分子和分母都可能有所不同.为了使事情变得更复杂,一些数据输入操作符可能在分子和分母之间的任何地方都加了空格!
所以我的输入可能是:
“ x / y”或“ x / y”或“ x / y”或“ x / y”或“ x / y” ….可能还有更多连击:(
在上述任何一种情况下,我都希望确定x和y是否为数字,以及它们之间是否有“ /”斜杠.正则表达式无望,请帮助
我用php编码,我猜preg_match是需要使用的.
谢谢阅读.
解决方法:
$pattern = "%(\d+) */ *(\d+)%";
$test = array(
'half is 1/2',
'13/100 is your score',
'only 23 /90 passed',
'no idea here:7/ 123',
'24 / 25',
'1a/2b'
);
foreach($test as $t){
if(preg_match($pattern, $t, $matches))
echo "PASS: n:$matches[1], d:$matches[2]\n";
else
echo "FAIL: $t\n";
}
输出:
PASS: n:1, d:2
PASS: n:13, d:100
PASS: n:23, d:90
PASS: n:7, d:123
PASS: n:24, d:25
FAIL: 1a/2b
标签:preg-match,php,regex 来源: https://codeday.me/bug/20191023/1913700.html