看一下位于以下位置的正则表达式:
https://regex101.com/r/hC3uJ7/1
当我在PHP上运行此正则表达式时,我只得到第一行,而不是外括号的全部内容。有人可以帮我解决这个问题。
我面临的问题是,当我在php中运行相同的代码时,得到的唯一匹配项是:
我注意到,“代码生成器”上regex101.com不逃避某些字符,尤其是-$
符号。
考虑到上述事实,您将获得所需的结果:
$re = "/^[^\\w\\s] ([\\w-,@\\.\\?\\(\\)\\*!;:=+&%\$£@\"'’ ]*(\\n[a-z-,@\\.\\?\\(\\)\\*!;:=+&%\$£@\"'’][\\w-,@\\.\\?\\(\\)\\*!;:=+&%\$£@\"'’ ]+)*)/mu";
$str = "1.37 Mark one answer\nYou must not use a hand-held phone while\ndriving. Using a hands-free system\n is acceptable in a vehicle with power\nsteering fljsadlfkjasldkfjlaksd lkfjasld\nlfksadjflkasjdflaksjdfalks lkfj sdlk\n will significantly reduce your field of\nvision\n will affect your vehicle's electronic\nsystems\n is still likely to distract your attention\nfrom the road\nWhile driving your concentration is required\nall the time. Even using a hands-free kit can\nstill distract your attention from the road.\nAny distraction, however brief, is potentially\ndangerous and could cause you to lose\ncontrol. Except in a genuine emergency, it\nis an offence to u";
preg_match_all($re, $str, $matches);
echo "<pre>";
var_dump($matches[0]);
// the output:
array(4) {
[0]=>
string(118) " is acceptable in a vehicle with power
steering fljsadlfkjasldkfjlaksd lkfjasld
lfksadjflkasjdflaksjdfalks lkfj sdlk"
[1]=>
string(48) " will significantly reduce your field of
vision"
[2]=>
string(47) " will affect your vehicle's electronic
systems"
[3]=>
string(58) " is still likely to distract your attention
from the road"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句