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
4

5 回答 5

11

答案是

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

如果您允许包含.--.不匹配的字符串。无论如何,您为什么要允许它们匹配?但是如果你真的需要这些字符串来匹配,一个可能的解决方案是

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

第一个正则表达式的单个.or-被一系列交替的.和替换-,以.or开头-,可选地后跟-.or.-对,可选地后跟 a -or .,以允许偶数个交替字符。这种复杂性可能是过冲,但似乎是当前规范所需要的。如果最多需要 2 个交替.并且-需要,则正则表达式变为

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

在这里这里测试

于 2012-05-26T07:55:03.590 回答
3

试试这个

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

更新 1

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

更新 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) «$»
-->

你可以在这里测试它。

于 2012-05-26T07:07:25.207 回答
1

这应该这样做:

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

它将确保单词以字母数字或下划线字符开头和结尾。中间的括号将确保一行中最多有一个句点或破折号,后跟至少一个字母数字或下划线字符。

于 2012-05-26T07:22:35.083 回答
0
/^[A-Z0-9_][A-Z0-9_.-]*[A-Z0-9_]$/i

这确保第一个和最后一个字符不是破折号或句点;中间的其余部分可能包含任何字符(在您选择的集合内)。

于 2012-05-26T07:15:24.760 回答
0

下面的正则表达式将检查任何包含字符、数字、破折号等的字符串,并且中间只有一个点。

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

希望这可以帮助

于 2012-05-26T07:22:04.973 回答