python 多行正则表达式捕获
python multiline regex capture
我有以下字符串:
hello
abcd
pqrs
123
123
123
我的 objective 是捕获从 hello 开始到第一次出现 123 的所有内容。
所以预期的输出是:
hello
abcd
pqrs
123
我使用了以下内容:
output=re.findall('hello.*123?',input_string,re.DOTALL)
但输出如下:
['hello\nabcd\npqrs\n123\n123\n123']
有没有办法使用 ?
对 123 进行非贪婪查找?或者有没有其他方法可以达到预期的输出?
为此尝试使用 lookhead
。您正在寻找一组字符后跟 \n123\n
:
import re
input_string = """hello
abcd
pqrs
123
123
123"""
output_string = re.search('[\w\n]+(?=\n123\n)', input_string).group(0)
print(output_string)
#hello
#abcd
#pqrs
#123
希望这有用。
我有以下字符串:
hello
abcd
pqrs
123
123
123
我的 objective 是捕获从 hello 开始到第一次出现 123 的所有内容。 所以预期的输出是:
hello
abcd
pqrs
123
我使用了以下内容:
output=re.findall('hello.*123?',input_string,re.DOTALL)
但输出如下:
['hello\nabcd\npqrs\n123\n123\n123']
有没有办法使用 ?
对 123 进行非贪婪查找?或者有没有其他方法可以达到预期的输出?
为此尝试使用 lookhead
。您正在寻找一组字符后跟 \n123\n
:
import re
input_string = """hello
abcd
pqrs
123
123
123"""
output_string = re.search('[\w\n]+(?=\n123\n)', input_string).group(0)
print(output_string)
#hello
#abcd
#pqrs
#123
希望这有用。