我有一个正则表达式:
/^(?:'[A-z](([\._\-][A-z0-9])|[A-z0-9])*[a-z0-9_]*')$/我正试着建造一个类似的,但仍然没有成功。
有关更多信息,它是用户名的正则表达式。以下是规则:
编辑:
我使用Codeigniter regex_match,它有一个bug,不接受管道。因此,等效的正则表达式不应该有管道。
我很感谢你的帮助。提前谢谢。
发布于 2014-01-29 20:05:40
^(?:[a-zA-Z](?:[a-zA-Z0-9._-]*[a-zA-Z0-9_]+)?)$http://regex101.com/r/jX2wY5
发布于 2014-01-29 20:04:00
假设您使用的是PCRE (或大部分)兼容语言,请尝试如下:
/^([A-Z][\w.-]*[A-Z_\d]|[A-Z])$/ihttp://regex101.com/r/hS7mB9
在javascript中:
var str = "john.m"; //example
console.log((/^([A-Z][\w.-]*[A-Z_\d]|[A-Z])$/i).test(str));在php中:
$str = "john.m"; //example
if (!preg_match("/^([A-Z][\w.-]*[A-Z_\d]|[A-Z])$/i", $str);没有|
$str = ".John.m";
if (preg_match("/^[A-Z]$/i", $str))
{
//check if it's one letter
}
else if (preg_match("/^[A-Z][\w.-]*[A-Z_\d]/i", $str))
{
//if not, check if it fits the other format
}
else
{
//we can assume that it's now failed the test
}https://stackoverflow.com/questions/21441363
复制相似问题