给出给定行中的短语,即使单词在行中具有不同数量的空格,我也必须能够匹配该短语。
因此,如果短语为"the quick brown fox"
并且行为"the quick brown fox jumped over the lazy dog"
,则"the quick brown fox"
的实例仍应匹配。
我已经尝试过的方法是用正则表达式模式将行中的所有空白实例替换为空白,但是,如果该行包含了正则表达式未将其视为文字的字符,则该方法并不总是有效。
答案 0 :(得分:1)
这应该有效:
import re
pattern = r'the\s+quick\s+brown\s+fox'
text = 'the quick brown fox jumped over the lazy dog'
match = re.match(pattern, text)
print(match.group(0))
输出为:
the quick brown fox
答案 1 :(得分:0)
您可以使用此正则表达式。选中here
(the\s+quick\s+brown\s+fox)
答案 2 :(得分:0)
您可以将给定的字符串用空格分开,然后用空格将它们重新连接起来,以便随后将其与您要查找的短语进行比较:
executionpolicy > $env:USERPROFILE\Desktop\executionpolicy.txt
答案 3 :(得分:0)
对于一般情况:
检查给定句子是否为替换后行的子字符串
import re
pattern = "your pattern"
for line in lines:
line_without_spaces= re.sub(r'\s+', ' ', line)
# will replace multiple spaces with one space
return pattern in line_without_spaces
答案 4 :(得分:0)
正如您稍后所澄清的,您需要匹配单词的任何行和系列。为了实现这一点,我添加了更多示例来阐明两个提议的类似正则表达式的作用:
text = """the quick brown fox
another line with single and multiple spaces
some other instance with six words"""
第一个匹配整行,遍历单行
pattern1 = re.compile(r'((?:\w+)(?:\s+|$))+')
for i, line in enumerate(text.split('\n')):
match = re.match(pattern1, line)
print(i, match.group(0))
其输出是:
0 the quick brown fox
1 another line with single and multiple spaces
2 some other instance with six words
第二个单词匹配单个单词,并在单个行上进行迭代,从而一个接一个地迭代它们:
pattern2 = re.compile(r'(\w+)(?:\s+|$)')
for i, line in enumerate(text.split('\n')):
for m in re.finditer(pattern2, line):
print(m.group(1))
print()
其输出是:
the
quick
brown
fox
another
line
with
single
and
multiple
spaces
some
other
instance
with
six
words