目前,我正在使用它:
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
}
例如,我想匹配:
pro.duct-name_
_pro.duct.name
p.r.o.d_u_c_t.n-a-m-e
但我不想匹配:
pro..ductname
.productname-
-productname.
-productname
答案 0 :(得分:9)
答案是
/^[a-zA-Z0-9_]+([-.][a-zA-Z0-9_]+)*$/
如果只允许包含.-
和-.
的字符串不匹配。无论如何,你为什么要允许它们匹配?但如果你真的需要这些字符串来匹配,可能的解决方案是
/^[a-zA-Z0-9_]+((\.(-\.)*-?|-(\.-)*\.?)[a-zA-Z0-9_]+)*$/
第一个正则表达式的单个.
或-
被替换为一系列交替的.
和-
,从.
或{{开始1}},可选地分别后跟-
或-.
对,可选地分别后跟.-
或-
,以允许偶数个交替的字符。这种复杂性可能是一种过冲,但目前的规范似乎需要这种复杂性。如果需要最多2个交替.
和.
,则正则表达式变为
-
答案 1 :(得分: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) «$»
-->
你可以测试它here。
答案 2 :(得分:1)
这应该做:
/^[A-z0-9_]([.-]?[A-Z0-9_]+)*[.-]?[A-z0-9_]$/
它将确保单词以字母数字或下划线字符开头和结尾。 中间的括号将确保连续最多只有一个句点或破折号,后跟至少一个字母数字或下划线字符。
答案 3 :(得分:0)
/^[A-Z0-9_][A-Z0-9_.-]*[A-Z0-9_]$/i
这可确保第一个和最后一个字符不是破折号或句号;中间的其余部分可能包含任何字符(在您选择的集合中)。
答案 4 :(得分:0)
下面的正则表达式将检查包含字符,数字,短划线等的任何字符串,并且中间只有一个点。
/^[A-Za-z0-9_-]+(\.){1}[A-Za-z0-9_-]+$/i
希望这会有所帮助