我有以下文字:
https://stackoverflow.com | https://google.com | first text to match |
https://randomsite.com | https://randomurl2.com | text | https://randomsite.com |
https://randomsite.com | https://randomsite.com |
我正在嘗試匹配不是 url 的字串的第一個序列,直到|. 在這個例子中,我希望正則運算式匹配:
https://stackoverflow.com | https://google.com | first text to match |
目前我有這個:
/^(.*)[|]\s(\b\w*\b)?\s[|]/gm
但是,這僅在不是 url 的第一個序列只是一個沒有空格的字串時才有效。如果first text to matchis just first,那么它將匹配。
期望的結果是匹配這兩種情況,不帶空格的字串和帶空格的匹配字串。
編輯:有時我還需要一個貪婪匹配,正則運算式將匹配所有內容,直到text |.
uj5u.com熱心網友回復:
如果您必須至少匹配一個前導 url:
\A[\s\S]*?\b\K(?:https?://\S*\h*\|\h*) [^\s|][^|\r\n]*\|
解釋
\A字串的開始[\s\S]*?盡可能少地匹配任何字符\b\K一個詞的邊界,然后忘記到目前為止匹配的是什么(?:https?://\S*\h*\|\h*)匹配一個或多個 url,后跟|可選空格[^\s|]匹配除管道外的非空白字符[^|\r\n]*可選擇匹配除管道或換行符之外的任何字符,然后匹配最后一個管道
正則運算式演示
如果沒有前導網址也可以:
\A[\s\S]*?\b\K(?:https?://\S*\h*\|\h*)*[^\s|][^|\r\n]*\|
正則運算式演示
例子
$re = '~\A[\s\S]*?\b\K(?:https?://\S*\h*\|\h*) [^\s|][^|\r\n]*\|~';
$str = ' https://stackoverflow.com | https://google.com | first text to match |
https://randomsite.com | https://randomurl2.com | text | https://randomsite.com |
https://randomsite.com | https://randomsite.com |';
if(preg_match($re, $str, $matches)) {
echo $matches[0];
}
輸出
https://stackoverflow.com | https://google.com | first text to match |
uj5u.com熱心網友回復:
你想包括空格
/^(.*)[|]\s(\b(\w|\s)*\b)?\s[|]/gm
如果您想在文本中允許各種特殊字符(包括換行符),您可以嘗試這種方法:
\|\s*((?!\s*\w :\/\/)[^|] ?)\s\|
https://regex101.com/r/2OOKky/1
如果您想在文本中允許各種特殊字符(但不允許換行),您可以嘗試這種方法:
(?:^|\|)(?:(?!$)\s) ((?!\s*\w :\/\/)(?:(?!$)[^|]) ?)(?:(?!$)\s)*\|
https://regex101.com/r/HS3bra/1
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485684.html
