用于匹配字母、数字和某些符号的正则表达式

时间:2021-04-06 21:39:39

I need to validate a username in PHP. It can be:

我需要在PHP中验证用户名。它可以是:

  • Letters (upper and lower case)
  • 字母(大小写)
  • Numbers
  • 数字
  • Any of these symbols :.,?!@
  • 这些符号中的任何一个:…?!@
  • Up to 15 characters OR 16 if the last character is one of the following #$^ (it can also be 15 or less with one of these 3 characters at the end only)
  • 15字符或16以下如果最后一个字符是一个# $ ^ 15或更少(也可以用一个最后3个字符)

How do I do this?

我该怎么做呢?

1 个解决方案

#1


4  

Start with this:

从这开始:

/^[a-zA-Z0-9:.,?!@]{3,15}[#$^]?$/

then refine it to your needs. Try to see if you need escaping of the special char, but you should get the idea.

然后根据您的需要进行改进。试着看看你是否需要转义这个特殊的字符,但是你应该明白它的意思。

This means: from a to z, from A to Z, from 0 to 9 and :.,?!@ repeated from 3 to 15 times, optionally followed by one among #$^

这意味着:从a到z,从a到z,从0到9和:,?!@重复3到15倍,有选择地之一# $ ^紧随其后

#1


4  

Start with this:

从这开始:

/^[a-zA-Z0-9:.,?!@]{3,15}[#$^]?$/

then refine it to your needs. Try to see if you need escaping of the special char, but you should get the idea.

然后根据您的需要进行改进。试着看看你是否需要转义这个特殊的字符,但是你应该明白它的意思。

This means: from a to z, from A to Z, from 0 to 9 and :.,?!@ repeated from 3 to 15 times, optionally followed by one among #$^

这意味着:从a到z,从a到z,从0到9和:,?!@重复3到15倍,有选择地之一# $ ^紧随其后