Home  >  Article  >  Backend Development  >  求个PHP正则

求个PHP正则

WBOY
WBOYOriginal
2016-06-06 20:52:41960browse

只能由“0-9a-zA-Z”和“-”组成,并且“-”不能在头尾出现,长度至少一位。
征求一下广大牛淫的意见。

回复内容:

只能由“0-9a-zA-Z”和“-”组成,并且“-”不能在头尾出现,长度至少一位。
征求一下广大牛淫的意见。

^[0-9a-zA-Z][0-9a-zA-Z\-]*[0-9a-zA-Z]$

上边的不对,修改一个:

^(?!-)[a-z0-9A-z-]*[a-z0-9A-z]$

不以 - 开头,且以字母数字结尾,长度可以是1或多。

/^(?!\-)[0-9a-zA-Z\-]*?[0-9a-zA-Z]{1}$/

^([0-9a-zA-Z]{1}[0-9a-zA-Z-]*?[0-9a-zA-Z]{1}|[0-9a-zA-Z]{1})$ 
/^[0-9a-zA-Z]+([0-9a-zA-z-]+[^-\W_])*$/

关键是一个字的时候 所以给他分组就可以了 这题谁在zhihu上问过
对了 域名就是这种格式的 不包括后缀

其实分开是很简单的事情,没必要非整一个很复杂低效的正则
!/^-/.test(string) && !/-$/.test(string) && /^[0-9a-zA-Z\-]+$/.test(string)

function test($s, $t){
    var_dump(preg_match('#^(?!-)[0-9a-zA-Z-]+(?
                            
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn