[英]Isolate leading portion of string before first hyphen and omit any trailing spaces from match
[英]How to match text before the first hyphen
我有以下字符串:
Citas de Esopo - Música - Belleza...
Citas de Lem, Stanislaw - Tortura, sueño...
我需要從第一個中提取“ Esopo”,從第二個中提取“ Lem,Stanislaw”。 我有以下正則表達式非常適合第二種情況:
/Citas de (.*) \-(.*)\.\.\./
但對於第一個案例,它返回“ Esopo-Música”。 如何編寫正則表達式,使其與第一個“-”出現前的文本匹配?
我使用preg_match然后匹配的第一個數組位置
if( preg_match($pattern, $title, $result) > 0 )
{
$title = $result[1];
}
您可以使用:
\bCitas de\h+([^-]+)\h+
碼:
if ( preg_match_all(/\bCitas de\h+([^-]+)\h+/, $input, $matches) )
print_r($matches[1]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.