正则表达式匹配最多6位数字,可以在数字之间包括1个破折号或1个空格。

huangapple go评论49阅读模式
英文:

Regex to match a maximum of 6 digits which may include 1 dash or 1 space anywhere between the digits?

问题

以下是您提供的内容的中文翻译:

我需要帮助:

\d{1,5}(-| )\d{1,5}

这是我正在处理的内容:

^([C][A|F|G|J|K|L])( |)(\d{1,6}|\d{1,5}(-| )\d{1,5})$

([C][A|F|G|J|K|L]) 匹配前两个字符。

( |) 匹配一个可选的空格。

\d{1,6} 匹配1-6个不带短划线或空格的数字。

\d{1,5}(-| )\d{1,5} 应匹配一个包含1个短划线或1个空格的2-6位数字。

我需要帮助的是最后一部分,但如果您知道更好的编写整个正则表达式的方法,那么它应该仅以'C'开头,第二个字符应该只能是'A''F''G''J''K''L'。在前两个字符之后,可以选择跟一个' '(空格)字符,然后是1-6位数字。

以下是我想要匹配整个正则表达式的示例:

CA 123456
CF 1
CK 0467 21
CL 4 2
CG 456-789
CJ 1-23

如果可能的话(可选),它还应该匹配:

CA546
CJ99-0
CK8 0565

它不应匹配:

CZ 123456
C 987-123
CA 12345-
CK  12
CJ -456
CL-654
CJ 1234567
英文:

I need help with this:

\d{1,5}(-| )\d{1,5}

This is what I'm working on:

^([C][A|F|G|J|K|L])( |)(\d{1,6}|\d{1,5}(-| )\d{1,5})$

([C][A|F|G|J|K|L]) matches the first 2 characters.

( |) matches an optional space.

\d{1,6} matches 1-6 digits without any dash or space.

\d{1,5}(-| )\d{1,5} is supposed to match a 2-6 digit number which includes either 1 dash or 1 space.

I need help with the last part, but if you know a better way to write the entire Regex, then it should only start with a 'C' and the second character should only be either 'A', 'F', 'G', 'J', 'K' or 'L'. After the first 2 characters, there can optionally be a ' ' (space) character, followed by the 1-6 digits.

Here are examples of what I want to match for the entire Regex:

CA 123456
CF 1
CK 0467 21
CL 4 2
CG 456-789
CJ 1-23

And, if possible (optional), it should also match:

CA546
CJ99-0
CK8 0565

It should NOT match:

CZ 123456
C 987-123
CA 12345-
CK  12
CJ -456
CL-654
CJ 1234567

答案1

得分: 3

你可以将你的正则表达式简化为这样,

^C[AFGJKL] ?(?=(?:\d+[ -]\d+$|\d{1,6}$))[\d -]{1,7}$

并实现你的期望结果

当你将字符放在字符集[]中时,不需要使用|,而单个字符[C]可以简单地写成C

写一个可选字符可以通过任何字符后跟一个?来实现,所以可选的空格可以写成 ?

唯一棘手的部分是如何以一种方式编写\d{1,5}(-| )\d{1,5},以便它要么仅匹配1到6个数字,要么如果数字之间有一个空格或连字符,则应该匹配一个额外的字符以包括该空格或连字符,以便最大匹配的字符变为7而不是6,这就是我将解释以下正则表达式部分的地方,

(?=(?:\d+[ -]\d+$|\d{1,6}$))[\d -]{1,7}
  • (?= - 正向先行断言,以便(?:\d+[ -]\d+$|\d{1,6}$)中的任何一个都应该匹配,这本质上表示,要么匹配(\d+[ -]\d+$)用空格或连字符分隔的一个或多个数字,再次匹配一个或多个数字,否则(\d{1,6}$)匹配1到6个数字
  • [\d -]{1,7} - 最后捕获数字、空格和连字符
英文:

You can simplify your regex to this,

^C[AFGJKL] ?(?=(?:\d+[ -]\d+$|\d{1,6}$))[\d -]{1,7}$

and achieve your Desired Results

You don't need | when you place characters in a character set [] and single character [C] can simply be written as C

Writing an optional character can be done by any character followed by a ? so optional space can be written as ?

Only tricky part is how do you write \d{1,5}(-| )\d{1,5} in a way so that it either matches only 1 to 6 digits or if there is a single space or hyphen between the digits, then it should match one more additional character to incorporate that space or hyphen so max matched characters become 7 instead of 6, which is where I will explain following regex part,

(?=(?:\d+[ -]\d+$|\d{1,6}$))[\d -]{1,7}
  • (?= - positive lookahead so that either of \d+[ -]\d+$ or \d{1,6}$ should match, which essentially says, either match (\d+[ -]\d+$) one or more digits separated with a space or hyphen and again one or more digits else just (\d{1,6}$) match 1 to 6 digits

  • [\d -]{1,7} - Finally just capture digits space and hyphen

答案2

得分: 2

你可以使用单个向前查看以确保没有连续7个数字。

然后匹配1个或更多数字,然后是一个可选部分,匹配空格或-,然后再次匹配1个或更多数字。

或者可以更具体一些,匹配在问题中指定的\d{1,5}个数字:

^C[AFGJKL](?! ?\d{7}) ?\d+(?:[ -]\d+)?$

解释

  • ^ 字符串的开头
  • C 字面匹配
  • [AFGJKL] 匹配列出的字符中的一个
  • (?! ?\d{7}) 负向前查看,断言后面不是一个可选空格,然后是7个连续的数字
  • ? 匹配一个可选空格
  • \d+(?:[ -]\d+)? 匹配1个或多个数字,后面是一个可选部分,匹配空格或-,然后再次匹配1个或多个数字
  • $ 字符串的结尾

可以查看正则表达式演示

英文:

You could use a single lookahead making sure that there are not 7 consecutive digits.

Then match 1+ digits followed by an optional part matching either a space or - and then again 1+ digits.

Or make it more specific, matching \d{1,5} digits as specified in the question

^C[AFGJKL](?! ?\d{7}) ?\d+(?:[ -]\d+)?$

Explanation

  • ^ Start of string
  • C Match literally
  • [AFGJKL] Match one of the listed characters
  • (?! ?\d{7}) Negative lookahead, assert not an optional space followed by 7 consecutive digits
  • ? Match an optional space
  • \d+(?:[ -]\d+)? Match 1+ digits with an optional part matching either a space or - followed by 1+ digits
  • $ End of string

See a regex demo.

答案3

得分: 1

我可能有错,但我认为正则表达式不提供定义最大长度并同时限制特定字符出现的功能。因此,您需要匹配所有可能的情况。

regex101-link

一些进一步的改进:

  • 为了使匹配器变为可选项,您可以使用x?,这使得匹配器或之前的组件成为可选项。
  • 当您想要匹配一组字符中的一个字符时,您可以创建一个新的字符类。[asdf]会匹配字母'a','s','d','f'中的任何一个。
英文:

I might be wrong, but I think regex doesn't provide a functionality to define a maximum length, and at the same time limit the occurrence of a specific char. Thus, you need to match all possible cases.
<br>

^(C[AFGJKL]) ?(\d{1,6}|\d{1,5}[- ]\d|\d{1,4}[- ]\d{1,2}|\d{1,3}[- ]\d{1,3}|\d{1,2}[- ]\d{1,4}|\d[- ]\d{1,5})$

regex101-link

some further improvements:

  • in order to make a matcher optional, you can use x?, which makes the matcher or the group before optional

  • when you want to match a char of a set of chars, you can just create a new character class. [asdf] would match any of the letters 'a', 's', 'd', 'f'.

huangapple
  • 本文由 发表于 2023年5月15日 03:38:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定