简体   繁体   中英

Converting a python lookahead assertion regex to valid Golang

I have the following regex that works well in Python (due to lookahead assertions).

some_list = re.findall('^(?=Name:)(.*?)(?=USB\\ Device\\ Filters:)', myinput, re.MULTILINE|re.DOTALL)

See example of myinput in the code block below.

Name: will always be the beginning of a group and USB Device Filters: will always be the end of a group. Not all lines have a valid key:value, eg, can have or a blank line.

Name:            Server1 10.0.0.11
Groups:          /
Guest OS:        Ubuntu (64-bit)
\n
<none>
USB Device Filters:

Name:            Server2 10.0.0.12
Groups:          /
Guest OS:        Debian (64-bit)
\n
<none>
USB Device Filters:

Can anyone help me convert this into a valid Golang regex?

The ultimate goal is to parse myinput and get a slice of matched groups.

Given this in Python:

^(?=Name:)(.*?)(?=USB Device Filters:)

Demo

Since ^(?=Name:) is a zero width assertion, Name: is captured by the capturing groups following it.

You can capture the same in Golang with this:

^(Name:.*?)USB Device Filters:

Demo

If you don't want to capture Name:\\s you can do:

^Name:\s*(.*?)USB Device Filters:

Demo

You don't need to escape the spaces with (?=USB\\ Device\\ Filters:) in either language. All have (?ms) flags set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM