python - Python多行匹配字符串结尾
问题描述
我正在尝试匹配,直到我点击一个模式(“this”忽略行首和模式之间的任何空格)或直到段落中字符串的结尾,使用:
r'.*?(?=^[^\S\n]*this|$)'
如果我的字符串只有一行($ 匹配行尾),则此正则表达式字符串可以正常工作。但是我找不到匹配字符串结尾的正则表达式,那么有没有一种干净的方法呢?以下是我的代码:
import re
a_str="""\
paragraph starts here
another line
this line may or may not exist"""
a_match = re.compile(r'.*?(?=^[^\S\n]*this|$)', re.MULTILINE|re.DOTALL).match(a_str)
编辑:
预期输出:
"paragraph starts here\nanother line"
解决方案
现在可以在多行模式下用 '\Z' 表示字符串的末尾。
推荐阅读
- matlab - 具有多个 X 轴的 Matlab 条形图
- javascript - 给定 2 xyz 和时间戳;导出速度
- compiler-errors - MPLAB X IDE v4.15 中的项目加载错误
- c++ - 为什么以及何时需要提供我自己的删除器?
- ios - WebKit 支持较低版本的 iOS
- mapbox - 无代码工作室中的 Mapbox 动画 SVG
- android - 我可以在 android Oreo 的 SQLite 中的特定时间插入数据,例如今晚 11:50 吗?
- java - 从 IntelliJ IDEA 中的文件夹打开文件
- html - 为 td 表 HTML 标记的 bgcolor 属性添加 if else-if else 块
- php - 无法从 php 中的 Intranet 获取用户名登录名