簡體   English   中英

正則表達式不會在PHP中輸出所有可能的匹配項

[英]Regular expression is not outputting all the potential matches in PHP

看一下位於的正則表達式:

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"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM