2013-02-19 142 views
0

什麼是正則表達式匹配字符串包含至少 3個散列用於分隔單詞?php正則表達式preg_match

匹配:

the-rain-in-spain 
the-rain-in-spain-is-falling-on-the-plain 

不匹配:

the-rain-in 
the---in 
+3

你試過自​​己做點什麼嗎? – 2013-02-19 05:18:14

回答

0

你可以使用這個表達式

\w+(-\w+){3,} 

\w類似於[a-zA-Z0-9_]

\w+比賽1對多角色

{3,}是匹配前面的3至多次

0

我不知道PHP的一個量詞,但,這是爲我工作

//string strtmp = "the-rain-in"; 
//string strtmp = "the-rain-in-spain"; 
    string strtmp = "the-rain-in-spain-is-falling-on-the-plain"; 
//string strtmp = "the---in"; 
    Match matchdec = Regex.Match(strtmp, @"\w+(-\w+){3,}", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);      
    if (matchdec.Success) 
     { 
      //your code 
     } 

希望這有助於