python - 无法通过正则表达式找到多行
问题描述
我无法使用正则表达式在组中的多行上找到匹配项
import re
text = "import alkfa dfa aldkf \n import my name is xyz \n i am
working incoverfox"
m = re.findall(r'^(import .+?$)', text, re.MULTILINE)
print(m)
预期结果:['import alkfa dfa aldkf', import my name is xyz] 实际结果:['import alkfa dfa aldkf']
解决方案
import re
text = "a import alkfa dfa aldkf \n import my name is xyz \n i am working incoverfox"
re.findall(r'(import .+)', text)
r'(import .+)'
将找到以 end 开头的字符串,import
除了\n
.
推荐阅读
- javascript - 每次应用加载后从 Service Worker 运行的 setInterval 的重复实例
- spring - Spring Boot 白名单 ip 范围
- javascript - nightwatch 中如何在所有 3 大浏览器的每个环境中运行测试?
- optimization - 有没有办法告诉编译器优化以减少分支?
- flutter - “文本选择索引被限制(-1-> 0)以保持在界限内”在我的描述字段中
- c++ - 使用 CMake 和 BOOST 时如何传入参数以更改单元测试的输出?
- neo4j - 如何构建图数据库来表示关系数据库?
- reactjs - 使用没有语法错误的第三方 React 库时出现 Unexpected Token 语法错误
- office365 - Office 365 在 ARCHIVE ROOT 上调用 EWS findFolders 时抛出奇怪的错误
- php - PHP org.json.JSONException 的 Android 问题:在字符 0 处输入结束