简体   繁体   English

可选字符,数字,符号正则表达式

[英]Optional Characters, Numbers, Symbols regex

The regex task that I am trying to achieve is pretty simple. 我要实现的正则表达式任务非常简单。 A string can contain one of the following: 字符串可以包含以下之一:

  • alphabet plus numbers 字母加数字
  • alphabet plus numbers plus symbols 字母加数字加符号
  • alphabet plus symbols 字母加符号

I have been able to achieve the first two points but I am struggling to get the last one. 我已经能够实现前两点,但是我在努力争取最后一点。

/^(?=.*[a-z])(?=.*[0-9]).{7,}$/

Strings that should match are as followed: 应该匹配的字符串如下:

  • test123 测试123
  • test123@#][ test123 @#] [
  • test@#;[-= 测试@#; [-=

Please note that for the last scenario when I say alphabet plus symbols . 请注意,对于最后一种情况,当我说alphabet plus symbols I'd like it to match any symbols. 我希望它可以匹配任何符号。 I did alternatively try: 我也尝试过:

/(.*).{7}/

And this did not work. 这没有用。 Reason being because that means an individual could enter just letters. 原因是因为这意味着个人只能输入字母。 So in order for the regex to satisfy it needs to be one of the following above as stated. 因此,为了使正则表达式,以满足它需要高于以下中的一个作为所述。

根据我的解释,这可能有效:

/^[a-zA-Z]+[^a-zA-Z]+$/

I sense that by "plus" you mean "then", so this should do it: 我感觉到“加号”表示“然后”,因此应该这样做:

^(?=.{7,})[a-zA-Z]+(?!$)[0-9]*[^a-zA-Z0-9]*$

This regex requires that if numbers and symbols are present, that number precede symbols. 此正则表达式要求,如果存在数字和符号,则该数字应位于符号之前。

Here "symbol" is defined as any character not a letter or number, you may want to instead list the characters that are "symbols", eg [@%#()*] 此处,“符号”定义为不是字母或数字的任何字符,您可能需要列出“符号”的字符,例如[@%#()*]

A negative look ahead is used to require that at least some characters must follow the letters. 否定前视用于要求字母后面至少包含一些字符。

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

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