2012-05-26 63 views
5

目前點劃線,我使用這樣的:正則表達式匹配的字母數字字符,下劃線,句號和破折號,只允許在中間

if (preg_match ('/^[a-zA-Z0-9_]+([a-zA-Z0-9_]*[.-]?[a-zA-Z0-9_]*)*[a-zA-Z0-9_]+$/', $product)) { 
return true; 
} else { 
return false 
} 

例如,我想匹配:

  1. pro.duct-name_
  2. _pro.duct.name
  3. p.r.o.d_u_c_t.n-a-m-e

但我並不想匹配:

  1. pro..ductname
  2. .productname-
  3. -productname.
  4. -productname
+1

有什麼問題嗎? –

+0

編輯示例,使其更易於理解。是否需要進一步解釋?請讓我知道,我會很樂意進一步澄清。 – banskt

+0

爲什麼'pro..ductname'不匹配?點在中間? –

回答

9

答案是

/^[a-zA-Z0-9_]+([-.][a-zA-Z0-9_]+)*$/ 

,只要你允許含有.--.不匹配的字符串。無論如何,你爲什麼要讓他們匹配?但如果你真的需要這些字符串匹配過,一個可能的解決方案是

/^[a-zA-Z0-9_]+((\.(-\.)*-?|-(\.-)*\.?)[a-zA-Z0-9_]+)*$/ 

.或第一正則表達式的-由交替.-,從無論是.-的序列代替,可選接着分別爲-..-對,可選地分別後接-.,以允許偶數個交替字符。這種複雜性可能是過沖,但似乎是目前的規格所需要的。如果需要的2交替.-一個最大,正則表達式變得

/^[a-zA-Z0-9_]+((\.-?|-\.?)[a-zA-Z0-9_]+)*$/ 

測試herehere

+0

第二個實際工作。非常感謝,但我必須承認,我不完全理解你的第二個正則表達式的順序。 – banskt

+0

我編輯了答案,以澄清 –

+0

感謝您的解釋。 – banskt

3

試試這個

(?im)^([a-z_][\w\.\-]+)(?![\.\-])\b 

UPDATE 1

(?im)^([a-z_](?:[\.\-]\w|\w)+(?![\.\-]))$ 

UPDATE 2

(?im)^([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)$ 

說明

<!-- 
(?im)^([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)$ 

Match the remainder of the regex with the options: case insensitive (i);^and $ match at line breaks (m) «(?im)» 
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
Match the regular expression below and capture its match into backreference number 1 «([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)» 
    Match a single character present in the list below «[a-z_]» 
     A character in the range between 「a」 and 「z」 «a-z» 
     The character 「_」 «_» 
    Match the regular expression below «(?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Match either the regular expression below (attempting the next alternative only if this one fails) «\.\-\w» 
     Match the character 「.」 literally «\.» 
     Match the character 「-」 literally «\-» 
     Match a single character that is a 「word character」 (letters, digits, and underscores) «\w» 
     Or match regular expression number 2 below (attempting the next alternative only if this one fails) «\-\.\w» 
     Match the character 「-」 literally «\-» 
     Match the character 「.」 literally «\.» 
     Match a single character that is a 「word character」 (letters, digits, and underscores) «\w» 
     Or match regular expression number 3 below (attempting the next alternative only if this one fails) «\-\w» 
     Match the character 「-」 literally «\-» 
     Match a single character that is a 「word character」 (letters, digits, and underscores) «\w» 
     Or match regular expression number 4 below (attempting the next alternative only if this one fails) «\.\w» 
     Match the character 「.」 literally «\.» 
     Match a single character that is a 「word character」 (letters, digits, and underscores) «\w» 
     Or match regular expression number 5 below (the entire group fails if this one fails to match) «\w» 
     Match a single character that is a 「word character」 (letters, digits, and underscores) «\w» 
Assert position at the end of a line (at the end of the string or before a line break character) «$» 
--> 

你可以測試它here

+1

\ w是不一樣的[A-ZA-Z0-9_] –

+0

的測試表明它不工作 –

+0

我不知道這是什麼@Walter指的是,但詳細一點,[在PHP手冊](http://www.php.net/manual/en/regexp.reference.escape.php)說:_「單詞」字符是任何字母或數字或下劃線字符,即任何可以成爲Perl「單詞」的一部分。字母和數字的定義由PCRE的字符表控制,並且如果發生特定於語言環境的匹配,則可能會有所不同。例如,在「FR」(法語)語言環境中,某些字符代碼大於128用於重音字母,這些都是由\ w._ – Herbert

0
/^[A-Z0-9_][A-Z0-9_.-]*[A-Z0-9_]$/i 

這確保第一個和最後一個字符不是短劃線或句點;在休息間可以由任何字符(您所選擇的組內)。

0

下面的正則表達式將檢查包含字符,數字,破折號等,並只有一箇中間點的任何字符串。

/^[A-Za-z0-9_-]+(\.){1}[A-Za-z0-9_-]+$/i 

希望這有助於

1

這應該這樣做:

/^[A-z0-9_]([.-]?[A-Z0-9_]+)*[.-]?[A-z0-9_]$/ 

這將確保字開頭,以字母或下劃線字符結束。 中間的支架將確保會有一排最多一個點或破折號,隨後在至少一個字母或下劃線。

相關問題