0

我需要一个正则表达式来匹配可以是小写字母或数字但必须至少包含一个小写字母的字符串。

因此,对于示例输入:

a123 b123 123c C123 aaa AAA 1234 B123 1234

匹配将是a123,b123和.123caaa

我在想这样的表达(\d*|[a-z]+)+,但不完全是。这将包括1234我不想要的也是非法的。

4

3 回答 3

6

我假设英文字母和 0-9 位数字:

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

如你看到的:

  • 该字符串只能包含小写英文字母字符 ( a-z) 或0-9数字。
  • 字符串必须有小写英文字母[a-z]。由于*0 或更多量词),其余部分是可选的。
于 2013-04-25T05:39:06.817 回答
1

怎么样:

^(?=.*[a-z])[a-z\d]+$

解释:

The regular expression:

(?-imsx:^(?=.*[a-z])[a-z\d]+$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  [a-z\d]+                 any character of: 'a' to 'z', digits (0-9)
                           (1 or more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-04-25T13:04:15.853 回答
1

尝试这样的事情:

/([a-z\d]*[a-z][a-z\d]*)/

至少需要一个小写字母,然后在其前后允许 0 个或多个小写字母和数字。

于 2013-04-25T05:41:44.233 回答