如何在python中使用正则表达式查找两个非字母数字字符之间的模式

我对 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 !


料青山看我应如是
浏览 230回答 3
3回答

蓝山帝景

您可以使用该re.DOTALL标志来.匹配换行符;不需要环顾四周:import res = '''*CHI:&nbsp; &nbsp;<that guy was> [//] that bunny was going to [: gonna] take that&nbsp; &nbsp; balloon !%mor:&nbsp; &nbsp;pro:dem|that n|bunny aux|be&PAST&13S part|go-PRESP&nbsp; &nbsp; part|go-PRESP~inf|to v|take pro:dem|that n|balloon !'''print(re.search(r'\*CHI:&nbsp; &nbsp;(.+)\n%mor:', s, re.DOTALL)[1])输出:<that guy was> [//] that bunny was going to [: gonna] take that&nbsp; &nbsp; balloon !

慕神8447489

转义 '*' 字符:re.findall(r'(?<=\*CHI:)[\s\S]*(?=%mor:)',&nbsp;s)正向后视 '?<=' 和正向前瞻 '?=' 将修剪您的开始和结束条款。[\s\S] 将处理换行符。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python