我对 python regex 很陌生。我无法清楚地了解如何在句子中搜索“*”,因为 * 在正则表达式中用作关键字,我感到很困惑。我的问题是,我有一个文件,其中:*CHI: <that guy was> [//] that bunny was going to [: gonna] take that balloon !%mor: pro:dem|that n|bunny aux|be&PAST&13S part|go-PRESP part|go-PRESP~inf|to v|take pro:dem|that n|balloon !所以在这里我必须检索“* CHI :”和“%mor”之间的句子我想要的输出应该是<that guy was> [//] that bunny was going to [: gonna] take that balloon !
3 回答
蓝山帝景
TA贡献1843条经验 获得超7个赞
您可以使用该re.DOTALL标志来.匹配换行符;不需要环顾四周:
import re
s = '''*CHI: <that guy was> [//] that bunny was going to [: gonna] take that
balloon !
%mor: pro:dem|that n|bunny aux|be&PAST&13S part|go-PRESP
part|go-PRESP~inf|to v|take pro:dem|that n|balloon !
'''
print(re.search(r'\*CHI: (.+)\n%mor:', s, re.DOTALL)[1])
输出:
<that guy was> [//] that bunny was going to [: gonna] take that
balloon !
慕神8447489
TA贡献1780条经验 获得超1个赞
转义 '*' 字符:
re.findall(r'(?<=\*CHI:)[\s\S]*(?=%mor:)', s)
正向后视 '?<=' 和正向前瞻 '?=' 将修剪您的开始和结束条款。[\s\S] 将处理换行符。
添加回答
举报
0/150
提交
取消