简体   繁体   English

使用 Python 正则表达式中的排列捕获重复子模式

[英]Capturing repeating sub-patterns with permutations in Python regex

I am trying to tokenize a string made of sub-patterns that can appear in any order.我试图标记一个由可以以任何顺序出现的子模式组成的字符串。 The sub-patterns are underscore, letters or numbers.子模式是下划线、字母或数字。 For example:例如:

   'ABC_123_DEF_456' would provide ('ABC', '_', '123', '_', 'DEF', '_', '456')

Here is the implemented regex giving the unexpected result:这是实现的正则表达式给出了意想不到的结果:

>>> m = regex.match(r'^((_)|(\d+)|([[:alpha:]]+))+$', 'ABC_123_DEF_456')
>>> m.groups()
('456', '_', '456', 'DEF')

Updates: - permutations: the three sub-patterns can appear in any order for example:更新: - 排列:三个子模式可以以任何顺序出现,例如:

'ABC123__' would provide ('ABC', '123', '_', '_')

You can use /([az]+|\\d+|_)/i to chunk the string into groups of digits, alphabetical characters or single underscores:您可以使用/([az]+|\\d+|_)/i将字符串分成数字组、字母组或单个下划线:

>>> re.findall(r"([a-z]+|\d+|_)", "ABC_123_DEF_456", re.I)
['ABC', '_', '123', '_', 'DEF', '_', '456']
>>> re.findall(r"([a-z]+|\d+|_)", "ABC123__", re.I)
['ABC', '123', '_', '_']

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

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