如何使用Python正则表达式来获取所有字符串行

2024-10-01 09:38:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要取EndOfSource.EndOfMessage.之间的所有字符串行。你知道吗

例如,如果输出为:

8019 Warning Microsoft-Windows-DNS-Client **EndOfSource.** The system failed to register host (A or AAAA) resource records (RRs) for network adapter
with settings:

  Adapter Name : {B50403AE-8D65-4933-9E8E-7149657E41CD}

   Host Name : l-reg-8128

 Primary Domain Suffix : mtl.labs.mlnx

 DNS server list :

 10.7.77.135, 10.4.0.121

 Sent update to server : <?>

 IP Address(es) :

  fdfd:fdfd:7:36:2e0:81ff:fe33:92ad, 10.7.38.128

The reason the system could not register these RRs was because of a security related problem. The cause of this could be (a) your computer does not have permissions to register and update the specific DNS domain name set for this adapter, or (b) there might have been a problem negotiating valid credentials with the DNS server during the processing of the update request.

You can manually retry DNS registration of the network adapter and its settings by typing 'ipconfig /registerdns' at the command prompt. If problems still persist, contact your DNS server or network systems administrator. See event details for specific error code information. **EndOfMessage.**

我尝试以下代码:

import re
re.findall("EndOfSource. (.*) EndOfMessage.", output)

Tags: orofthetoregisterforadapterserver
1条回答
网友
1楼 · 发布于 2024-10-01 09:38:46

这里有几个问题:

  • .*是一个贪婪的子模式,当您需要懒惰的子模式时,请使用.*?
  • 默认情况下,.与换行符不匹配,需要使用re.Sre.DOTALL启用点对点模式
  • 点必须转义以匹配文字点。你知道吗

使用

re.findall(r"EndOfSource\. (.*?) EndOfMessage\.", output, flags=re.S)

相关问题 更多 >