odi assle odi assle - 5 months ago 18
Python Question

how to get all string lines using regular expression in python

I need to take all string line between

EndOfSource.
and
EndOfMessage.
.

For example if output is:

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.**


I try the following code:

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

Answer

There are several issues here:

  • The .* is a greedy subpattern, while you need the lazy one, use .*?
  • The . does not match a newline by default, you need to enable a DOTALL mode with re.S or re.DOTALL
  • Dots must be escaped to match literal dots.

Use

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