簡體   English   中英

PHP正則表達式preg_match

[英]php regex preg_match

匹配包含至少 3個用於分隔單詞的哈希的字符串的正則表達式是什么?

火柴:

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

不匹配:

the-rain-in
the---in

您可以使用此正則表達式

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

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

\\w+匹配1到多個字符

{3,}是一個將前面的第3組匹配多次的量詞

我不了解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
       }

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM