如何在 re.split() 函数中对正则表达式进行分组? [英] How to group a regular expression in re.split() function?
本文介绍了如何在 re.split() 函数中对正则表达式进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看两个例子 re.split() 是如何工作的:
<预><代码>>>>re.split(',', 'a,b')['a', 'b']但是
<预><代码>>>>re.split('(,)', 'a,b')['a', ',', 'b']为什么我在列表中得到,"?如何避免?
我在问,因为我想使用类似于xy(a|b)cd"的表达式进行拆分.
解决方案
使用非捕获组,例如:
re.split('(?:,)', 'a,b')
See two examples how re.split() works:
>>> re.split(',', 'a,b')
['a', 'b']
but
>>> re.split('(,)', 'a,b')
['a', ',', 'b']
Why I get ',' in the list? How to avoid it?
I am asking, because I would like to make a split using an expression similar to 'xy(a|b)cd'.
解决方案
Use a non-capturing group, like:
re.split('(?:,)', 'a,b')
这篇关于如何在 re.split() 函数中对正则表达式进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文