使用
我正在尝试将一段文本拆分为格式如下的文件中:
module
some text
endmodule
module
some other text
endmodule
在单词模块和结束模块之间,并且仍然在输出字符串中包含模块和结束模块。
这不是其他正则表达式问题的副本,因为我正在尝试使用 re.split(( 返回列表,而不是查找。
这是我尝试过的正则表达式
s=file.read()
l=re.split("module(.*)endmodule",s)
但它不会分裂任何东西...
理想情况下,最终输出将是一个包含两个模块作为字符串的列表,
['模块 sometext endmodule', 'modulesomeothertext endmodule']
我的猜测是,您可能想要设计一个类似于以下内容的表达式:
module(.*?)endmodule
不过不知道。
使用 re.finditer 进行测试
import re
regex = r"module(.*?)endmodule"
test_str = ("module n"
"some textn"
"endmodulenn"
"module n"
"some other textn"
"endmodule")
matches = re.finditer(regex, test_str, re.DOTALL)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
使用re.findall
进行测试
import re
regex = r"module(.*?)endmodule"
test_str = ("module n"
"some textn"
"endmodulenn"
"module n"
"some other textn"
"endmodule")
print(re.findall(regex, test_str, re.DOTALL))
如果您希望进一步探索或简化/修改它,则在此演示的右上角面板上解释了该表达式,在此链接中,如果您愿意,可以逐步观看它如何与一些示例输入匹配。
我们可以使用积极的回顾和积极的展望,如
print(re.split('(?<=endmodule)[.n]*?(?=module)', s))
给
['modulensome textnendmodule', 'modulensome other textnendmodule']
哪里
s = ("modulen"
"some textn"
"endmodulenn"
"modulen"
"some other textn"
"endmodule")