简体   繁体   English

正则表达式检查字符串只包含字母和数字,但不仅包括数字

[英]regex to check the string contains only letter and numbers but not only numbers

I need a help with regex which checks the string contains only letter and numbers but not only numbers 我需要一个正则表达式的帮助,它检查字符串只包含字母和数字,但不仅仅是数字

Valid 有效

* letters
* 1wret
* 0123chars
* chars0123
* cha2rs

Invalid 无效

* 1324
* xcvxxc%$#
* xcv123xxc%$#
* _012chars
* _test

Here are the components of the regex we're going to use: 以下是我们将要使用的正则表达式的组件:

  • ^ and $ are the beginning and end of the string anchors respectively ^$分别是字符串锚点的开头和结尾
  • \\d matches a digit \\d匹配一个数字
  • [a-zA-Z] matches a letter [a-zA-Z]匹配一封信
  • [a-zA-Z\\d] matches a letter or a digit [a-zA-Z\\d]匹配字母或数字
  • * is "zero-or-more" repetition *是“零或多”重复

With these, we can now compose the regex we need ( see on rubular.com ): 有了这些,我们现在可以构成我们需要的正则表达式( 参见rubular.com ):

^\d*[a-zA-Z][a-zA-Z\d]*$

Here's an explanation of the pattern: 这是对模式的解释:

from the beginning...  till the end
|                      |
^\d*[a-zA-Z][a-zA-Z\d]*$
 \_/\______/\_________/

The 3 parts are: 这三部分是:

  • Maybe some digits as a prefix... 也许一些数字作为前缀......
  • But then definitely a letter! 但绝对是一封信!
  • And then maybe some digits and letters as a suffix 然后可能会有一些数字和字母作为后缀

References 参考

This should do it: 这应该这样做:

^[0-9]*[a-zA-Z]+[a-zA-Z0-9]*$

This requires at least one character of [a-zA-Z] . 这需要[a-zA-Z]中的至少一个字符。

[a-z0-9]*[a-z]+[a-z0-9]*

Instead of using a regular expression, you can also use the ctype_*() functions: 您也可以使用ctype_*()函数代替使用正则表达式:

var_dump(ctype_alnum('letters') && !ctype_digit('letters'));     // bool(true)
var_dump(ctype_alnum('0123chars') && !ctype_digit('0123chars')); // bool(true)
var_dump(ctype_alnum('1324') && !ctype_digit('1324'));           // bool(false)
var_dump(ctype_alnum('xcvxxc%$#') && !ctype_digit('xcvxxc%$#')); // bool(false)

But if you want a regular expression, you can use this: 但是如果你想要一个正则表达式,你可以使用这个:

var_dump(preg_match('/^[a-z0-9]*[a-z]+[a-z0-9]*$/i', $input));
^([a-zA-Z0-9]*)([a-zA-Z]+)([a-zA-Z0-9]*)$

^(?=.*[az])[a-zA-Z0-9]+$

  • (?=.*[az]) positive lookahead to make sure that there is at least one letter in the string (but doesn't consume any characters - gives up the matched characters as soon as it returns (in this case, as soon as it finds a letter)). (?=.*[az])正向前瞻以确保字符串中至少有一个字母(但不消耗任何字符) - 一旦返回就放弃匹配的字符(在这种情况下,尽快因为它找到了一封信))。
  • [a-zA-Z0-9]+ make sure string contains only alphanumeric characters. [a-zA-Z0-9]+确保字符串仅包含字母数字字符。
  • ^ and $ are start and end of string delimiters. ^$是字符串分隔符的开头和结尾。

Personally (I hate regex and find them generally to be hard to maintain), I'd do it in two steps. 就个人而言(我讨厌正则表达式并且发现它们通常难以维护),我会分两步完成。

  1. Is it all alphanumeric? 这都是字母数字吗?
  2. Is there at least one letter? 至少有一封信吗?
([0-9]*[a-zA-Z]+)+
^[0-9]*[a-zA-Z]+[0-9a-zA-Z]*$

翻译:从头开始,匹配0或更多数字,然后匹配至少一个字母,然后匹配零个或多个字母或数字,直到结束。

这应该工作^([a-zA-Z0-9]*)([a-zA-Z]+)([a-zA-Z0-9]*)$ EDIT sry得到了*而且+搞砸了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM