简体   繁体   English

正则表达式部分匹配几个不同的组

[英]regex partial match for several different groups

I need regexp to match string build from several groups (A is any letter, 9 is any digit): 我需要regexp来匹配来自多个组的字符串构建(A是任何字母,9是任何数字):

group 1 regex [AZ]{1,2}[0-9]? 第1组正则表达式[AZ] {1,2} [0-9]?

A
A9
AA9

group 2 regex [AZ]{1,3}[0-9]? 第2组正则表达式[AZ] {1,3} [0-9]?

A
AA
AAA
AAA9

group 3 regex [AZ]{2,3}[0-9]?[AZ]? 第3组正则表达式[AZ] {2,3} [0-9]?[AZ]?

AAA
AA9
AA9A

group 4 regex [0-9]{1,2}[AZ]{1,2}[0-9]? 第4组正则表达式[0-9] {1,2} [AZ] {1,2} [0-9]?

9A
9AA
9A9
99A9

Not each group must be present but there must be all in correct order - I mean (digit is group number): 并非每个组都必须存在,但是所有组都必须以正确的顺序排列-我的意思是(数字是组号):

1
12
123
1234

So if there is present group 3 there must me all preceding groups present also. 因此,如果存在第3组,那么所有前面的组也必须存在。

As there are four groups (can be more), so alternative like 由于有四个组(可以更多),所以替代方案就像

^[A-Z]{1,2}[0-9]{1}|[A-Z]{1,2}[0-9]{1}\s{1}[A-Z]{1}[0-9]?$

is not the best option as it would be complicated and difficult to maintain. 并非最佳选择,因为它很复杂且难以维护。 Is there any solution with groups or something? 团体或其他解决方案吗? The order of groups is important. 组的顺序很重要。

This regex will match all the strings you have provided: 此正则表达式将匹配您提供的所有字符串:

^[A-Z]+[0-9]*(\s+[A-Z]+[0-9]*)+$

and unlimited words. 和无限的单词。

怎么样:

^[A-Z]{1,2}[0-9]?(?:\s+[A-Z]{1,3}[0-9]?(?:\s+[A-Z]{2,3}[0-9]?[A-Z]?(?:\s+[0-9]{1,2}[A-Z]{1,2}[0-9]?)?)?)?$

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

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