python - Python Regular Expression for pattern containing multiple lines
问题描述
I want to extract all the text printed after "AAAAAAAAAAAAAAAAAA"
Give me some text!
AAAAAAAAAAAAAAAAAA
S
p
p
p
Epppp
The following does not work:
import re
m = re.findall(r'AAAAAAAAAAAAAAAAAA(.*)', result)
print m[0]
Also, can I specify a variable in a regular expression instead of a hard coded string: "AAAAAAAAAAAAAAAAAA"?
Reason being, the text: "AAAAAAAAAAAAAAAAAA" is a variable and changes. So, I would like to look for a specific variable value in the pattern and then extract all the text after it.
解决方案
Use re.S
or re.DOTALL
(they are synonyms) to have findall
match across lines. Or, in your case, search
is probably more appropriate since you only want one match. Also, to have it work for a non-hard-coded string, simply use string formatting or string concatenation. To avoid having unescaped regex characters in the string, run it through re.escape
.
import re
result = """Give me some text!
AAAAAAAAAAAAAAAAAA
S
p
p
p
Epppp"""
s = 'AAAAAAAAAAAAAAAAAA'
# With formatting
m = re.search(r'{}(.*)'.format(re.escape(s)), result, re.S)
# With concatenation
m = re.search(re.escape(s) + r'(.*)', result, re.S)
print m.group(1)
推荐阅读
- java - OneSignal:收到通知时 Android 应用程序崩溃
- python - Python 中 10 倍交叉验证代码中的错误
- react-native-android - 找不到模块'@babel/core' react-native
- wordpress - 如何更改wordpress文件结构?
- django - psycopg2.ProgrammingError:列“...”不存在 - 通过 makemigrations 错误?
- python - 错误的 NumPy 数组计算结果
- php - 发送数组 UTF8 CURL
- jms - Spring JMS:发送和接收Java对象时如何拦截结果JSON转换?
- tensorflow - Tensorflow,带有自定义过滤器的卷积
- java - 将servlet响应的响应传回上一个jsp页面