Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用换行符查找字符串的其余部分_Python_Regex_String_Match_Newline - Fatal编程技术网

Python 使用换行符查找字符串的其余部分

Python 使用换行符查找字符串的其余部分,python,regex,string,match,newline,Python,Regex,String,Match,Newline,我有一个像这样的字符串 msg = "abc 123 \n 456" m = re.match('abc (.*)',msg) 我想做这样的事情 msg = "abc 123 \n 456" m = re.match('abc (.*)',msg) 并让m.groups返回“123\n 456” 但目前它只返回“123” 如何捕获字符串的其余部分,而不是仅仅捕获到行的末尾,使用s(dotall)修饰符强制点匹配所有字符,包括换行符 >>> import re >

我有一个像这样的字符串

msg = "abc 123 \n  456"
m = re.match('abc (.*)',msg)
我想做这样的事情

msg = "abc 123 \n  456"
m = re.match('abc (.*)',msg)
并让m.groups返回“123\n 456”

但目前它只返回“123”

如何捕获字符串的其余部分,而不是仅仅捕获到行的末尾,使用
s
(dotall)修饰符强制点匹配所有字符,包括换行符

>>> import re
>>> msg = "abc 123 \n  456"
>>> m = re.match(r'(?s)abc (.*)', msg)
>>> m.group(1)
'123 \n  456'
您需要使用该标志,否则
正则表达式原子将不匹配换行符

re.DOTALL:
使
特殊字符与任何字符匹配,包括换行符;如果没有此标志,
将匹配除换行符以外的任何内容

所以这应该是你想要的:

m = re.match('abc (.*)', msg, re.DOTALL)