[英]Checking some expressions in the paragraphs using regular expressions with PHP
我想在PHP中使用正則表達式來檢查文本是否至少有兩個段落,第一段以“實際上”開頭,另一段以“我建議”開頭。
例如,我測試了這個:
$text = "In fact Paris is more beautiful than Berlin.
I suggest we go to Paris this summer."
$value = preg_match ( "/ (^ In fact (.)) (I suggest). * /", $text);
echo $value;
I get $value = 0;
我得到$ value = 0;
我不知道為什么,請幫忙。
您需要添加修飾符\\s
和量詞*
( 示例 )
(^In fact (.*))\s(I suggest(.*))
\\s
匹配任何空白字符,如\\n
和\\r
\\n
,換行符串。 您的代碼現在應該如下所示:
$value = preg_match ( "/(^In fact (.*))\s(I suggest(.*))/", $text);
現在$value
將為1。
這應該工作。
$text = "In fact Paris is more beautiful than Berlin.
I suggest we go to Paris this summer.";
if(preg_match ( "/^In fact(.*)I suggest/s", $text)){
echo 'true';
} else {
echo 'false';
}
在你的正則表達式中. *
. *
允許任何一個字符.
然后任意數量的空格*
。 這個(.)
允許一個空格。
我沒有看到你想捕獲的內容,所以我刪除了你的捕獲組。 s
修飾符允許.
匹配新線。
正則表達式演示: https : //regex101.com/r/vW2oZ8/1
PHP演示: https : //eval.in/584773
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.