Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 Reg ex需要一直读取到行尾\n_Python_Regex - Fatal编程技术网

Python Reg ex需要一直读取到行尾\n

Python Reg ex需要一直读取到行尾\n,python,regex,Python,Regex,下面是我的byte类,我正在尝试使用下面的正则表达式读取数据直到Suj stderr=b'ok: [localhost] => () fatal: [localhost]: Failed! => {"changed":true,"stdout":"Error": Invalid user or pwd\nJoin AD\n Suj}' 正则表达式# 但上面的reg ex在第一个“\n”之前给出响应,但我想一直读到最后,即第二个\n。我不确定遗漏了什么,如果有任何线索,我们将不胜感激

下面是我的byte类,我正在尝试使用下面的正则表达式读取数据直到Suj

stderr=b'ok: [localhost] => () fatal: [localhost]: Failed! => {"changed":true,"stdout":"Error": Invalid user or pwd\nJoin AD\n Suj}'
正则表达式#

但上面的reg ex在第一个“\n”之前给出响应,但我想一直读到最后,即第二个
\n
。我不确定遗漏了什么,如果有任何线索,我们将不胜感激

所需的响应类似于“changed”:true,“stdout”:“Error”:无效用户或pwd\nJoin AD\n Suj}。但我得到的响应类似于“changed”:true,“stdout”:“Error”:无效用户或pwd\n join AD”


感谢您的时间

问题在于使用了点字符,默认情况下,点字符与新行不匹配。使用
DOTALL
标志更改:

stderr=b'ok: [localhost] => () fatal: [localhost]: Failed! => {"changed":true,"stdout":"Error": Invalid user or pwd\nJoin AD\n Suj}'
exp = re.compile(r'fatal: \[localhost\]: Failed! =>(.*)[\n$](.*)', re.DOTALL)
res = exp.search(stderr.decode('utf-8'))
res.group() # Results in the following: 
结果:

'fatal: [localhost]: Failed! => {"changed":true,"stdout":"Error": Invalid user or pwd\nJoin AD\n Suj}'

问题在于点字符的使用,默认情况下,点字符与新行不匹配。使用
DOTALL
标志更改:

stderr=b'ok: [localhost] => () fatal: [localhost]: Failed! => {"changed":true,"stdout":"Error": Invalid user or pwd\nJoin AD\n Suj}'
exp = re.compile(r'fatal: \[localhost\]: Failed! =>(.*)[\n$](.*)', re.DOTALL)
res = exp.search(stderr.decode('utf-8'))
res.group() # Results in the following: 
结果:

'fatal: [localhost]: Failed! => {"changed":true,"stdout":"Error": Invalid user or pwd\nJoin AD\n Suj}'
尝试
re.search(r'fatal:\[localhost\]:失败!=>(.*)(?:[\n$](.*)+),stderr.decode('utf-8')
try
re.search(r'fatal:\[localhost\]:失败!=>(.*)(?:[\n$](.*)+),stderr.decode('utf-8')