正则表达式用于固定长度的字符串,从多个单词开始

时间:2022-12-14 21:39:46

I'm trying to make a regex (JS flavor) that matches a string that is exactly 17 alphanumeric characters in length and must start with either "AB, "DE" or "GH". After these 3 possibilities, any alphanumeric character is accepted.

我正在尝试创建一个regex (JS风格),它匹配的字符串长度为17个字母数字字符,必须以“AB”、“DE”或“GH”开头。在这三种可能性之后,任何字母数字字符都被接受。

Match:

匹配:

AB163829F13246915
DET639601BA167860
GHF1973771A002957

Don't match

不匹配

XYZ63829F13246915
AAA639601BA167860
BBC1973771A002957

So far I have this regex which I'm testing on http://regexpal.com/

到目前为止,我在http://regexpal.com/上测试这个regex

^(AB|)[a-zA-Z0-9]{17}$

Not sure why the pipe character is required for it to match my first example, or why it fails when I add "DE" after the pipe.

不确定为什么需要管道字符来匹配我的第一个示例,或者为什么在管道之后添加“DE”时失败。

Anyone?

有人知道吗?

6 个解决方案

#1


10  

Use this:

用这个:

^(AB|DE|GH)[a-zA-Z0-9]{15}$

The first two characters already take up two, so you only need 15 more alphanumeric characters after that.

前两个字符已经占用了两个字符,所以在那之后您只需要15个字母数字字符。

http://rubular.com/r/rAWmIy4Xeh

http://rubular.com/r/rAWmIy4Xeh

#2


4  

You had it almost:

你几乎把它:

(AB|DE|GH)[a-zA-Z0-9]{15}

Demo

演示

Since AB|DE|GH will already be 2-char long, only 15 must be allowed beyond.

因为AB|DE|已经是2-char,所以只能允许15个。

You can also use a non-capturing group ((?:AB|DE|GH)[a-zA-Z0-9]{15}) and anchor your pattern (^(?:AB|DE|GH)[a-zA-Z0-9]{15}$) if needed.

您还可以使用无组((?:AB | | GH)[a-zA-Z0-9]{ 15 })和锚定你的模式(^(?:AB | | GH)[a-zA-Z0-9]{ 15 } $)。

#3


2  

you can try this:

你可以试试这个:

/^(?:AB|DE|GH)[A-Z0-9]{15}$/

#4


0  

Your current regex is looking for a string that may or may not start with AB, followed by some alphanumeric characters. If it does start with AB, the string length will be 19. Otherwise, it's 17.

您当前的regex正在寻找一个字符串,该字符串可能以AB开头,也可能不以AB开头,后面跟着一些字母数字字符。如果它以AB开头,那么弦的长度就是19。否则,它的17个。

Clearly, this is not what you meant.

显然,这不是你的意思。

Try this:

试试这个:

/^(?=AB|DE|GH)[a-zA-Z0-9]{17}$/

This uses a lookahead assertion to ensure the string starts with the right combinaions, then effectively starts over as it counts out the 17 characters.

这使用了一个前瞻断言,以确保字符串以正确的组合字开头,然后在计数出17个字符时有效地重新开始。

#5


0  

Try the following:

试试以下:

^(AB|DE|GH)[a-zA-Z0-9]{15}$

Whatever is matched by (AB|DE|GH) will consume exactly two characters, so you need to change the repetition on the [a-zA-Z0-9] character class to 15.

任何与之匹配的(AB|DE|GH)将只消耗两个字符,因此需要将[a-zA-Z0-9]字符类的重复次数更改为15。

Note that this can also be shortened a bit:

注意,这也可以缩短一点:

^(AB|DE|GH)[^\W_]{15}$

This works because \W matches everything except letters, numbers, and underscores. Putting \W and _ in a negated character class gives you an element that will only match letters and numbers.

这样做是因为除了字母、数字和下划线外,所有的东西都匹配。将\W和_放在一个被否定的字符类中,您将得到一个只匹配字母和数字的元素。

#6


0  

In your pattern, the pipe makes the first part of it matches either AB or an empty string. You haven't excluded the two characters from the 17, so it has to match the empty string to get the next 17 characters to match the rest of the pattern. The pattern will also match for example AB00000000000000000, i.e. AB followed by 17 alphanumerics.

在您的模式中,管道使其第一部分匹配AB或空字符串。您还没有从这17个字符中排除这两个字符,所以它必须匹配空字符串才能获得接下来的17个字符来匹配模式的其余部分。该模式也将匹配,例如ab0000000000000000000000000,即AB后面跟着17个字母数字。

You should make it match the combinations of two characters at the start, then 15 alphanumeric characters:

您应该使它在开始时匹配两个字符的组合,然后是15个字母数字字符:

^(AB|DE|GH)[a-zA-Z0-9]{15}$

#1


10  

Use this:

用这个:

^(AB|DE|GH)[a-zA-Z0-9]{15}$

The first two characters already take up two, so you only need 15 more alphanumeric characters after that.

前两个字符已经占用了两个字符,所以在那之后您只需要15个字母数字字符。

http://rubular.com/r/rAWmIy4Xeh

http://rubular.com/r/rAWmIy4Xeh

#2


4  

You had it almost:

你几乎把它:

(AB|DE|GH)[a-zA-Z0-9]{15}

Demo

演示

Since AB|DE|GH will already be 2-char long, only 15 must be allowed beyond.

因为AB|DE|已经是2-char,所以只能允许15个。

You can also use a non-capturing group ((?:AB|DE|GH)[a-zA-Z0-9]{15}) and anchor your pattern (^(?:AB|DE|GH)[a-zA-Z0-9]{15}$) if needed.

您还可以使用无组((?:AB | | GH)[a-zA-Z0-9]{ 15 })和锚定你的模式(^(?:AB | | GH)[a-zA-Z0-9]{ 15 } $)。

#3


2  

you can try this:

你可以试试这个:

/^(?:AB|DE|GH)[A-Z0-9]{15}$/

#4


0  

Your current regex is looking for a string that may or may not start with AB, followed by some alphanumeric characters. If it does start with AB, the string length will be 19. Otherwise, it's 17.

您当前的regex正在寻找一个字符串,该字符串可能以AB开头,也可能不以AB开头,后面跟着一些字母数字字符。如果它以AB开头,那么弦的长度就是19。否则,它的17个。

Clearly, this is not what you meant.

显然,这不是你的意思。

Try this:

试试这个:

/^(?=AB|DE|GH)[a-zA-Z0-9]{17}$/

This uses a lookahead assertion to ensure the string starts with the right combinaions, then effectively starts over as it counts out the 17 characters.

这使用了一个前瞻断言,以确保字符串以正确的组合字开头,然后在计数出17个字符时有效地重新开始。

#5


0  

Try the following:

试试以下:

^(AB|DE|GH)[a-zA-Z0-9]{15}$

Whatever is matched by (AB|DE|GH) will consume exactly two characters, so you need to change the repetition on the [a-zA-Z0-9] character class to 15.

任何与之匹配的(AB|DE|GH)将只消耗两个字符,因此需要将[a-zA-Z0-9]字符类的重复次数更改为15。

Note that this can also be shortened a bit:

注意,这也可以缩短一点:

^(AB|DE|GH)[^\W_]{15}$

This works because \W matches everything except letters, numbers, and underscores. Putting \W and _ in a negated character class gives you an element that will only match letters and numbers.

这样做是因为除了字母、数字和下划线外,所有的东西都匹配。将\W和_放在一个被否定的字符类中,您将得到一个只匹配字母和数字的元素。

#6


0  

In your pattern, the pipe makes the first part of it matches either AB or an empty string. You haven't excluded the two characters from the 17, so it has to match the empty string to get the next 17 characters to match the rest of the pattern. The pattern will also match for example AB00000000000000000, i.e. AB followed by 17 alphanumerics.

在您的模式中,管道使其第一部分匹配AB或空字符串。您还没有从这17个字符中排除这两个字符,所以它必须匹配空字符串才能获得接下来的17个字符来匹配模式的其余部分。该模式也将匹配,例如ab0000000000000000000000000,即AB后面跟着17个字母数字。

You should make it match the combinations of two characters at the start, then 15 alphanumeric characters:

您应该使它在开始时匹配两个字符的组合,然后是15个字母数字字符:

^(AB|DE|GH)[a-zA-Z0-9]{15}$