简体   繁体   English

正则表达式验证带有可选空格的字母数字字符串

[英]Regex to validate an alphanumeric string with optional space

I have been working on a regex for validating an alphanumeric string with the rules as below:我一直在研究一个正则表达式来验证一个字母数字字符串,规则如下:

  1. The first FOUR starting characters must be numbers and last TWO characters must be alphabets.四个起始字符必须是数字,最后两个字符必须是字母。

  2. The space is OPTIONAL but must be placed between two characters, meaning trailing space is not allowed.空格是可选的,但必须放在两个字符之间,这意味着不允许有尾随空格。

  3. The length of postal code must be 6 characters if SPACE is not included and 7 characters if space is included .如果包括空间邮政编码的长度必须是如果不包括空间6个字符7个字符

Eg.例如。

  • 1111 ZZ第1111章
  • 111 1ZZ 111 1ZZ
  • 1 111ZZ 1 111ZZ
  • 1111ZZ 1111ZZ

I tried using ^[0-9]{4}[A-Za-z]{2}$|^(?=[\\d|\\D]+ [\\d|\\D]+).{7}$ but this also validates 9999 1A as TRUE which should actually be FALSE.我尝试使用^[0-9]{4}[A-Za-z]{2}$|^(?=[\\d|\\D]+ [\\d|\\D]+).{7}$但这也验证了9999 1A为 TRUE,实际上应该是 FALSE。

Any leads or help will be appreciated :)任何线索或帮助将不胜感激:)

I suggest simplifying the problem ahead of time, by reducing all white spaces, which you seem to be uninterested in anyway:我建议提前简化问题,减少所有你似乎不感兴趣的空格:

var candidate = input.replaceAll(/\s/mg, '');

Then the regex is simply: /^\\d{4}[A-Za-z]{2}$/然后正则表达式很简单: /^\\d{4}[A-Za-z]{2}$/

If, however, you need to validate, that there actually are no leading or trailing white spaces, you can validate that ahead of time, and return a negative result right away.但是,如果您需要验证实际上没有前导或尾随空格,您可以提前验证,并立即返回否定结果。

(?=^.{6,7}$)^(([0-9] ?){4}( ?[a-zA-Z]){2})$

will match会匹配

  • 1111 ZZ第1111章
  • 111 1ZZ 111 1ZZ
  • 1 111ZZ 1 111ZZ
  • 1111ZZ 1111ZZ
  • 1111 ZZ第1111章

but not但不是

  • 9999 1A 9999 1A
  • 11111 Z 11111 Z
  • 1111111 1111111
  • 11 11 ZZ 11 11 ZZ

https://regex101.com/r/lByOx6/1 https://regex101.com/r/lByOx6/1

EDIT: explanation编辑:解释

The "Positive Lookahead" part: “正向前瞻”部分:

  • (?=^.{6,7}$) this only matches if the string meets the requirements, BUT it does not 'consume' the characters. (?=^.{6,7}$)这仅在字符串满足要求时才匹配,但它不会“消耗”字符。
    • . . is any character是任何字符
    • {6,7} is about repetitions {6,7} 是关于重复的

so (?=^.{6,7}$) is matched if the string has 6 or 7 characters, no matter what所以(?=^.{6,7}$)如果字符串有 6 或 7 个字符,则匹配,无论如何

Then the following part already 'consumes' the string to say that I want at the start 4 repetitions of numbers and optionally space, and at the end 2 repetitions of letters and optionally space.然后下面的部分已经“消耗”了字符串,说我想要在开始时重复 4 次数字和可选的空格,并在末尾重复 2 次字母和可选的空格。 The second part would accept strings such as 1 1 1 1 ZZ but as those are more than 7 characters, the first part wouldn't let the string match.第二部分将接受诸如1 1 1 1 ZZ字符串,但由于这些字符串超过 7 个字符,因此第一部分不会让字符串匹配。

Another option is to check if the string contains an optional space between the first and the last non whitespace character.另一种选择是检查字符串在第一个和最后一个非空白字符之间是否包含可选的空格。

Then match the first digit followed by 3 digits separated by an optional space and 2 or 3 times a char a-zA-Z or a space.然后匹配第一个数字,后跟由可选空格分隔的 3 个数字和 2 或 3 个字符 a-zA-Z 或空格。

Using a case insensitive match:使用不区分大小写的匹配:

^(?=\S+ ?\S+$)\d(?: ?\d){3}[A-Z ]{2,3}$

Explanation解释

  • ^ Start of string ^字符串开始
  • (?= Positive lookahead, assert what on the right is (?=正向前瞻,断言右边是什么
    • \\S+ ?\\S+$ Match optional space between the first and the last non whitespace char \\S+ ?\\S+$匹配第一个和最后一个非空白字符之间的可选空格
  • ) Close lookahead )关闭前瞻
  • \\d(?: ?\\d){3} Match a digit and repeat 3 times an optional space and a digit \\d(?: ?\\d){3}匹配一个数字并重复 3 次可选空格和一个数字
  • [a-zA-Z ]{2,3} Match 2-3 times either a char a-zA-Z or a space [a-zA-Z ]{2,3}匹配 2-3 次字符 a-zA-Z 或空格
  • $ End of string $字符串结尾

Regex demo正则表达式演示

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

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