这是一次非常直接的尝试。我没有使用python太长时间。似乎工作,但我相信我有很多东西需要学习。有人告诉我,如果我离开这里。需要找到模式,写出匹配的第一行,然后为匹配模式和返回修改后的字符串的剩余连续行添加摘要消息。
要明确......正则表达式.*Dog.*
需要
Cat
Dog
My Dog
Her Dog
Mouse
并返回
Cat
Dog
::::: Pattern .*Dog.* repeats 2 more times.
Mouse
#!/usr/bin/env python
#
import re
import types
def remove_repeats (l_string, l_regex):
"""Take a string, remove similar lines and replace with a summary message.
l_regex accepts strings and tuples.
"""
# Convert string to tuple.
if type(l_regex) == types.StringType:
l_regex = l_regex,
for t in l_regex:
r = ''
p = ''
for l in l_string.splitlines(True):
if l.startswith('::::: Pattern'):
r = r + l
else:
if re.search(t, l): # If line matches regex.
m += 1
if m == 1: # If this is first match in a set of lines add line to file.
r = r + l
elif m > 1: # Else update the message string.
p = "::::: Pattern '" + t + "' repeats " + str(m-1) + ' more times.\n'
else:
if p: # Write the message string if it has value.
r = r + p
p = ''
m = 0
r = r + l
if p: # Write the message if loop ended in a pattern.
r = r + p
p = ''
l_string = r # Reset string to modified string.
return l_string
答案 0 :(得分:1)
重新匹配功能似乎可以满足您的需求:
def rematcher(re_str, iterable):
matcher= re.compile(re_str)
in_match= 0
for item in iterable:
if matcher.match(item):
if in_match == 0:
yield item
in_match+= 1
else:
if in_match > 1:
yield "%s repeats %d more times\n" % (re_str, in_match-1)
in_match= 0
yield item
if in_match > 1:
yield "%s repeats %d more times\n" % (re_str, in_match-1)
import sys, re
for line in rematcher(".*Dog.*", sys.stdin):
sys.stdout.write(line)
在您的情况下,最终字符串应为:
final_string= '\n'.join(rematcher(".*Dog.*", your_initial_string.split("\n")))
答案 1 :(得分:1)
更新您的代码以提高效率
#!/usr/bin/env python
#
import re
import types
def remove_repeats (l_string, l_regex):
"""Take a string, remove similar lines and replace with a summary message.
l_regex accepts strings/patterns or tuples of strings/patterns.
"""
# Convert string/pattern to tuple.
if not hasattr(l_regex, '__iter__'):
l_regex = l_regex,
ret = []
last_regex = None
count = 0
for line in l_string.splitlines(True):
if last_regex:
# Previus line matched one of the regexes
if re.match(last_regex, line):
# This one does too
count += 1
continue # skip to next line
elif count > 1:
ret.append("::::: Pattern %r repeats %d more times.\n" % (last_regex, count-1))
count = 0
last_regex = None
ret.append(line)
# Look for other patterns that could match
for regex in l_regex:
if re.match(regex, line):
# Found one
last_regex = regex
count = 1
break # exit inner loop
return ''.join(ret)
答案 2 :(得分:0)
首先,你的正则表达式将比你从贪婪的比赛中停下来的速度慢得多。
.*Dog.*
相当于
Dog
但后者匹配得更快,因为不涉及回溯。字符串越长,“狗”出现的次数越多,因此正则表达式引擎必须做的回溯工作越多。事实上,“。* D”实际上保证了回溯。
那说,怎么样:
#! /usr/bin/env python
import re # regular expressions
import fileinput # read from STDIN or file
my_regex = '.*Dog.*'
my_matches = 0
for line in fileinput.input():
line = line.strip()
if re.search(my_regex, line):
if my_matches == 0:
print(line)
my_matches = my_matches + 1
else:
if my_matches != 0:
print('::::: Pattern %s repeats %i more times.' % (my_regex, my_matches - 1))
print(line)
my_matches = 0
目前尚不清楚非邻近比赛会发生什么。
还不清楚在非匹配线包围的单线匹配会发生什么。将“Doggy”和“Hula”附加到输入文件中,您将获得匹配消息“0”次。