[英]preg_match_all with special characters
我正在嘗試對包含要提取的多個短語的數據執行preg_match_all。
數據:
'us/Llane/Hówl' then some other text then 'us/Casey/Hówl' and so on
我想將兩個名稱Llane和Casey提取到數組中。 我目前正在使用http://www.phpliveregex.com/以及我的代碼本身來嘗試解決此問題,但是即使使用Internet上的一些出色指南,正則表達式也很難理解。 據我所知,這應該起作用:
preg_match_all("/us\/(.*?)\/HÓWL'/",$data,$output);
但是我所得到的只是$ output [0]和$ output [1]這兩個都是空白。 我之前沒有問題,所以它可能是特殊字符,但是我只能在preg_match_all上找到用於檢測特殊字符的信息,而不僅僅是在字符串中使用它們。 任何幫助都將是非常有用的,我已經連續4天被困在這個問題上,並且花費了大量時間來解決它。
您正在嘗試匹配HÓWL
而不是Hówl
..
$data = "'us/Llane/Hówl' then some other text then 'us/Casey/Hówl' and so on";
preg_match_all("~us/(.*?)/Hówl~", $data, $output);
print_r($output[1]);
輸出量
Array
(
[0] => Llane
[1] => Casey
)
另外,除非您知道Hówl
始終位於正斜杠的右側,否則我將考慮使用完整的Letter Unicode屬性 \\p{L}
。 這也將允許您匹配重音字符。
preg_match_all("~us/(.*?)/\p{L}+~u", $data, $output);
不區分大小寫可能無法正常工作
用這個:
$regex = '~us/\K.*?(?=/Hówl)~';
$count = preg_match_all($regex, $yourstring, $matches);
if($count) print_r($matches[0]);
比賽:
Llane
Casey
在演示中查看比賽。
說明
us/
匹配文字字符 \\K
告訴引擎放棄與最終匹配相距甚遠的匹配,它返回 .*?
懶惰地匹配到... (?=/Hówl)
可以斷言接下來是Hówl
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.