python正则表达式查找以数字为中心的子字符串并生成组合

2024-04-24 05:29:45 发布

您现在位置:Python中文网/ 问答频道 /正文

this question的帮助之后,我有一个正则表达式:

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}(?:[^\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

请帮我修改这个正则表达式。我仍然希望它完成A点和B点,而不是C点,我希望获得所有25个0-4个尾随词和起始词的序列组合。你知道吗

这是我想要的结果:

"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"

理想情况下,这将使用初始regex完成。你知道吗


Tags: samplere情况序列数字this单词regex
1条回答
网友
1楼 · 发布于 2024-04-24 05:29:45

让我们看看伪代码中的一个解决方案,这样我们就不会陷入细节中,特别是我可能误解了规范

据我所知,您要生成的组合将围绕轴心点构建。你知道吗

  1. 确定每个支点和最长的“翅膀”。为此,构建一个与(G1)(Pivot)(G2)匹配的正则表达式,其中G1和G2是围绕轴心允许的最长扩展(“翅膀”)。你知道吗
  2. 在代码中用两个for循环构建组合。在代码中,将G1和G2拆分为单词,并用两个嵌套的for循环构建所有可能的组合。你知道吗

Pivot和Wings的示例Regex

the demo中,可以检查组1、2和3。轴心是第一个1.2。左翼是AA AA AA AA。右翼是BB 1.2 BB 1.3 BB BB BB BB。你知道吗

注意,通过使.*?贪婪,我们获得了不同的匹配。在这种情况下,轴心是1.3,左翼是AA AA AA AA 1.2 BB 1.2 BB,右翼是BB BB BB BB(参见demo)。你知道吗

这里有一些代码让你开始。你知道吗

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!

相关问题 更多 >