表示“至少一个非数字”的正则表达式

时间:2021-03-18 11:08:40

I want to validate usernames according to this schema:

我想根据这个模式验证用户名:

  1. Allowable characters: letters, numbers, hyphen, underscore
  2. 允许字符:字母、数字、连字符、下划线
  3. First character must be a letter or a number
  4. 第一个字符必须是字母或数字。
  5. The username cannot be all numbers
  6. 用户名不能是所有的数字

This regular expression satisfies 1 and 2 above, but I can't figure out how to satisfy 3:

这个正则表达式满足上面的1和2,但是我不知道如何满足3:

/^[a-zA-Z\d][\w\-]+$/

(I'm using Ruby, if that's relevant)

(如果相关的话,我用的是Ruby)

5 个解决方案

#1


10  

Not very efficient, but simple:

不是很有效率,但是很简单:

/^(?!\d+$)[a-zA-Z\d][\w\-]+$/

/ ^(? ! \ d + $)[a-zA-Z \ d](\ w \]+ /美元

The lookahead simply means: "what follows isn't a string of numbers that go on until the end".

展望只是意味着:“接下来的不是一串一直到最后的数字”。

#2


1  

Not ideal, but easy: Use a second pass with the regex /^.*[a-zA-Z_\-].*$/

不理想,但容易:使用第二个通过正则表达式/ ^。*[a-zA-Z_ \]。* /

Just ensure it passes both and you'll be fine.

只要确保它同时通过,你就会没事。

#3


1  

If you can go with two passes, a simpler and faster second pass regexp is:

如果你可以进行两次传球,一个更简单、更快的二次传球regexp是:

/[^\d]/

This just matches anything that is not a number and it needs to match only one and it terminates early. You don't really need to be strict here because the first pass already rejects non-allowable characters.

它只匹配任何不是数字的东西,它只需要匹配一个,它会提前终止。这里不需要太严格,因为第一次遍历已经拒绝不允许的字符。

#4


0  

I would use the regex that you need for validation and then something like:

我将使用您需要的regex进行验证,然后类似:

passwd.to_i.to_s.length != passwd.length

to verify that passwd is not a string of digits after it passes the primary validation.

要验证passwd不是经过主验证后的一串数字。

#5


0  

Yet another way, though it may not perform as well as Max's:

还有另一种方式,尽管它可能不如马克斯的好:

/^[a-z0-9][-\w]*[-_a-z][-\w]*$/i

#1


10  

Not very efficient, but simple:

不是很有效率,但是很简单:

/^(?!\d+$)[a-zA-Z\d][\w\-]+$/

/ ^(? ! \ d + $)[a-zA-Z \ d](\ w \]+ /美元

The lookahead simply means: "what follows isn't a string of numbers that go on until the end".

展望只是意味着:“接下来的不是一串一直到最后的数字”。

#2


1  

Not ideal, but easy: Use a second pass with the regex /^.*[a-zA-Z_\-].*$/

不理想,但容易:使用第二个通过正则表达式/ ^。*[a-zA-Z_ \]。* /

Just ensure it passes both and you'll be fine.

只要确保它同时通过,你就会没事。

#3


1  

If you can go with two passes, a simpler and faster second pass regexp is:

如果你可以进行两次传球,一个更简单、更快的二次传球regexp是:

/[^\d]/

This just matches anything that is not a number and it needs to match only one and it terminates early. You don't really need to be strict here because the first pass already rejects non-allowable characters.

它只匹配任何不是数字的东西,它只需要匹配一个,它会提前终止。这里不需要太严格,因为第一次遍历已经拒绝不允许的字符。

#4


0  

I would use the regex that you need for validation and then something like:

我将使用您需要的regex进行验证,然后类似:

passwd.to_i.to_s.length != passwd.length

to verify that passwd is not a string of digits after it passes the primary validation.

要验证passwd不是经过主验证后的一串数字。

#5


0  

Yet another way, though it may not perform as well as Max's:

还有另一种方式,尽管它可能不如马克斯的好:

/^[a-z0-9][-\w]*[-_a-z][-\w]*$/i