表示字符串:
string = 'Other unwanted text here and start here: This is the first sentence.\nIt is the second one.\nNow, this is the third one.\nThis is not I want.\n'
我想摘录前三句话
This is the first sentence.\nIt is the second one.\nNow, this is the third one.
显然,以下正则表达式不起作用:
re.search('(?<=This)(.*?)(?=\n)', string)
在This
和第三个\n
之间提取文本的正确表达式是什么?你知道吗
谢谢。你知道吗
(?s)(This.*?)(?=\nThis)
使
.
包含换行符为(?s)
,查找以This
开头,后跟\nThis
的序列。你知道吗别忘了,搜索结果的
__repr__
不会打印整个匹配的字符串,所以您需要Jerry是对的,regex不是合适的工具,有更简单更有效的方法来解决这个问题
输出:
如果您只是想练习使用regex,那么遵循教程会容易得多。你知道吗
您可以使用这个正则表达式捕获以
This
文本开头的三个句子Demo
编辑:
Python代码
指纹
相关问题 更多 >
编程相关推荐