我有一个单词列表,有些是单字,有些是多字,这些单词可能有数字字符,也可能没有数字字符。你知道吗
举个例子-
word_list=['word', 'kap1','another word', 'another-1 word', 'another word 1']
我想确定列表中的单字条目,形式如下-
alphabets*Junction*digit(s)
其中连接可以是空格、连字符或无。例如,在上面的列表中,kap1
限定(没有其他条目限定)。现在,找到这个条目后,我想创建这个条目的变体(基于连接),并将它们添加到列表中。你知道吗
例如,在找到kap1
之后,我想将kap 1
和kap-1
添加到列表中。你知道吗
我能写出识别条目的初始正则表达式-
Word_NumberRegex=re.compile(r"^[a-zA-Z]+[ -]?\d+$")
但是我想知道一个好的算法来创建变体,这取决于连接。你知道吗
您可以使用3个捕获组并使用中间组捕获连接角色。使用连接字符搜索分隔符列表并获得所需的输出:
输出:
Code Demo
使用
re
,您可以捕获匹配的模式,并使用自定义分隔符重新格式化:预编译模式:
相关问题 更多 >
编程相关推荐