简体   繁体   English

我如何结合这两个正则表达式?

[英]How can I combine these two regexes?

I would like to combine the following two regexes into one: 我想将以下两个正则表达式合并为一个:

  • ^([AZ]{1,2}) ?([0-9]{1,4})$ eg AB 1234 ^([AZ]{1,2}) ?([0-9]{1,4})$例如AB 1234
  • ^([0-9]{1,4}) ?([AZ]{1,2})$ eg 1234 AB ^([0-9]{1,4}) ?([AZ]{1,2})$例如1234 AB

I thought this would be a simple as this: 我觉得这很简单:

^([AZ]{1,2}) ?([0-9]{1,4})|([0-9]{1,4}) ?([AZ]{1,2})$

However, the above always returns 4 groups, eg 但是,上面总是返回4组,例如

  1. ''
  2. ''
  3. '1234'
  4. 'AB'

How can I combine these two regexes in a way that will always returns two groups? 如何以一种始终返回两个组的方式组合这两个正则表达式? eg 例如

  1. '1234'
  2. 'AB'

or 要么

  1. 'AB'
  2. '1234'

Firstly your combination is not quite right, because the ^ only applies to the first alternative and the $ only applies to the second one. 首先,你的组合不太正确,因为^仅适用于第一种选择,而$仅适用于第二种选择。 So you need to group the alternation: 所以你需要对交替进行分组:

^(?:([A-Z]{1,2}) ?([0-9]{1,4})|([0-9]{1,4}) ?([A-Z]{1,2}))$

Now what you want to achieve can not be done with all regex engines, but some (eg PCRE) support a special alternation construct where capturing groups are counted individually for all alternations. 现在你想要实现的不是所有的正则表达式引擎,但是有些(例如PCRE)支持一种特殊的交替构造,其中捕获组被单独计算用于所有交替。 This is the syntax: 这是语法:

^(?|([A-Z]{1,2}) ?([0-9]{1,4})|([0-9]{1,4}) ?([A-Z]{1,2}))$

EDIT: 编辑:

Unfortunately, this is in particular not supported by Python. 不幸的是,Python特别不支持这一点。 Neither is the alternative of reusing a named capturing group. 也不是重用命名捕获组的替代方法。 Hence you will probably have to filter empty strings out of match.groups() or stick to two regular expressions. 因此,您可能必须从match.groups()过滤出空字符串,或者坚持使用两个正则表达式。

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

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