python正则表达式查找以数字为中心的子字符串并生成组合
在上的帮助之后,我有一个正则表达式: A.查找包含数字的单词,例如python正则表达式查找以数字为中心的子字符串并生成组合,python,regex,string,digit,Python,Regex,String,Digit,在上的帮助之后,我有一个正则表达式: A.查找包含数字的单词,例如1.2(但这不是最终匹配…) B.如果可能,将匹配从左到右扩展到另一个包含单词的数字,只要每个包含单词的数字之间不超过三个非数字单词 C.将匹配向左和向右扩展,以包含4个非数字单词的序列 sample = "AA AA AA AA AA AA 1.2 BB 1.2 BB 1.3 BB BB BB BB BB" matches = re.findall(r'(?=(\s(?:[^\d\s]+[\s]+){4}(?:[
1.2
(但这不是最终匹配…)
B.如果可能,将匹配从左到右扩展到另一个包含单词的数字,只要每个包含单词的数字之间不超过三个非数字单词
C.将匹配向左和向右扩展,以包含4个非数字单词的序列
sample = "AA AA AA AA AA AA 1.2 BB 1.2 BB 1.3 BB BB BB BB BB"
matches = re.findall(r'(?=(\s(?:[^\d\s]+[\s]+){4}(?:[^\d\s]*\d+(?:[^\d\s]+[\s]+){1,3}?)*?[^\d\s]*\d+.*?(?:[\s]+[^\d\s]+){4}\s))', sample)
匹配:AA 1.2 BB 1.2 BB 1.3 BB BB
请帮我修改这个正则表达式。我仍然希望它完成A点和B点,而不是C点,我希望获得所有25个0-4个尾随词和起始词的序列组合
sample = "AA AA AA AA AA AA 1.2 BB 1.2 BB 1.3 BB BB BB BB BB"
matches = re.findall(r'(?=(\s(?:[^\d\s]+[\s]+){4}(?:[^\d\s]*\d+(?:[^\d\s]+[\s]+){1,3}?)*?[^\d\s]*\d+.*?(?:[\s]+[^\d\s]+){4}\s))', sample)
这是我想要的输出:
"AA AA AA AA 1.2 BB 1.2 BB 1.3 BB BB BB BB"
"AA AA AA 1.2 BB 1.2 BB 1.3 BB BB BB BB"
"AA AA 1.2 BB 1.2 BB 1.3 BB BB BB BB"
"AA 1.2 BB 1.2 BB 1.3 BB BB BB BB"
"1.2 BB 1.2 BB 1.3 BB BB BB BB"
"AA AA AA AA 1.2 BB 1.2 BB 1.3 BB BB BB"
"AA AA AA 1.2 BB 1.2 BB 1.3 BB BB BB"
"AA AA 1.2 BB 1.2 BB 1.3 BB BB BB"
"AA 1.2 BB 1.2 BB 1.3 BB BB BB"
"1.2 BB 1.2 BB 1.3 BB BB BB"
"AA AA AA AA 1.2 BB 1.2 BB 1.3 BB BB"
"AA AA AA 1.2 BB 1.2 BB 1.3 BB BB"
"AA AA 1.2 BB 1.2 BB 1.3 BB BB"
"AA 1.2 BB 1.2 BB 1.3 BB BB"
"1.2 BB 1.2 BB 1.3 BB BB"
"AA AA AA AA 1.2 BB 1.2 BB 1.3 BB"
"AA AA AA 1.2 BB 1.2 BB 1.3 BB"
"AA AA 1.2 BB 1.2 BB 1.3 BB"
"AA 1.2 BB 1.2 BB 1.3 BB"
"1.2 BB 1.2 BB 1.3 BB"
"AA AA AA AA 1.2 BB 1.2 BB 1.3"
"AA AA AA 1.2 BB 1.2 BB 1.3"
"AA AA 1.2 BB 1.2 BB 1.3"
"AA 1.2 BB 1.2 BB 1.3"
"1.2 BB 1.2 BB 1.3"
理想情况下,这将通过使用初始正则表达式来实现。让我们看看伪代码中的解决方案,这样我们就不会陷入细节中,尤其是因为我可能误解了规范 据我所知,您要生成的组合将围绕轴心点构建
(G1)(Pivot)(G2)
匹配的正则表达式,其中G1和G2是围绕Pivot允许的最长扩展(“翅膀”)1.2
。左翼是AA
。右翼为BB 1.2 BB 1.3 BB
请注意,通过使*?
贪婪,我们可以获得不同的匹配。在这种情况下,枢轴为1.3
,左翼为AA 1.2 BB 1.2 BB
,右翼为BB BB
(请参阅)
这里有一些代码可以帮助您开始
import re
subject = "AA AA AA AA AA AA 1.2 BB 1.2 BB 1.3 BB BB BB BB BB"
myregex = r"""(?x)
( # Start Group 1: the left wing
# The four first words
(?:[^\d\s]+[ ]){4}
# The optional digit-non-digit groups
(?:
\S*\d\S*[ ] # a digit-containing word
(?:[^\d\s]+){1,3}[ ] # 1 to 3 non-digit words
)*?
) # Close Group 1 (the left wing)
# The Pivot
(\S*\d\S*)
( # Start Group 2: the right wing
# Get to the Last Required Digit Word
(?:
(?:[ ][^\d\s]+){1,3} # 1 to 3 non-digit words
[ ]\S*\d\S* # a digit-containing word
)*
# The four last words
(?:[ ][^\d\s]+){4}
) # End Group 2: the right wing
"""
match = re.search(myregex,subject)
leftwing = match.group(1)
pivot = match.group(2)
rightwing = match.group(3)
wordregex = re.compile("\S+")
print("Left Wing Tokens: ",wordregex.findall(leftwing))
print("Pivot: ", pivot)
print("Left Wing Tokens: ",wordregex.findall(rightwing))
print("Now it's up to you to write the loops to build the combinations!")
输出
Left Wing Tokens: ['AA', 'AA', 'AA', 'AA']
Pivot: 1.2
Left Wing Tokens: ['BB', '1.2', 'BB', '1.3', 'BB', 'BB', 'BB', 'BB']
Now it's up to you to write the loops to build the combinations!
一般来说,对于引号、paren或其他任何东西,你都不能很好地使用正则表达式嵌套。因此,也许我们的方法是找到上面带有正则表达式的字符串,然后手动将其拆分?嘿,已经有一段时间了,我不记得旧正则表达式是做什么的,所以我们可以重新开始吗?我不理解这个问题中的要求(输出中的组合代表什么,生成它们的“英语公式”)。你能详细解释一下吗?这个问题已经有4票接近,我担心它会被关闭,因为人们不理解你的问题(例如我不理解…)嘿,你的问题已经关闭了。我试图编辑它来澄清它。一旦你觉得它是完全清楚的,你可以写信给关闭它的人,考虑重新打开它,或者为它慢化。FY:添加示例代码让你开始。谢谢,尽管它在不同的字符串下崩溃了,比如包含相邻数字的字符串。无论如何,我已经不再尝试纯正则表达式的方法,而是使用正则表达式和嵌套for循环的组合来获得解决方案。