查看位于以下位置的正则表达式:
https://regex101.com/r/hC3uJ7/1
当我在PHP上运行此正则表达式时,我只获得第一行而不是外部括号的完整内容。有人可以帮我解决这个问题。
我面临的问题是,当我在php中运行相同的代码时,我得到的唯一匹配是:
答案 0 :(得分:1)
我注意到 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"
}