[英]preg_match will not ignore new line breaks
任務:找到10個單詞,忽略新的換行符。
我為multi-line
添加了m
標志,但沒有效果。
<?php
$string = "this is line one\n this is line two\n this is line three\n";
$pattern = '/([A-Za-z0-9\.]+ ){1,10}([A-Za-z0-9\.]+)/m';
preg_match($pattern, $string, $matches);
echo $matches[0];
echo "\n";
?>
結果:
this is line one
預期結果:
this is line one this is line two this is line
為了達到理想的結果,我只是手工刪除了新的換行符。
盡管這個理想的結果也不是完美的..因為它總共是11個單詞而不是10個單詞。如果我走了這么遠,我仍然會越來越接近我的使命。
這是找到10個單詞的正則表達式。
進行一場比賽,然后使用空格加入1-10組。
(?s)(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S).*?(?<!\\S)([A-Za-z0-9\\.]+)(?!\\S)
我的猜測是,也許您正在嘗試使用雙界量詞編寫一些表達式,例如:
(?i)^(\s*[a-z0-9. ]{1,10}(?=\s|$)){0,4}$
要么
(?i)^(\s*[a-z0-9. ]{1,10}(?=\s|$)){0,5}
要么
(?i)(\s*[a-z0-9. ]{1,10}(?=\s|$)){0,5}
要么
(?:\S+(?=\s|$)\s*){6}
要么
(?:\S+\s*){6}
或例如捕獲前六個單詞,
^(?:(\S+)\s*)(?:(\S+)\s*)(?:(\S+)\s*)(?:(\S+)\s*)(?:(\S+)\s*)(?:(\S+)\s*)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.