简体   繁体   English

将python前瞻断言正则表达式转换为有效的Golang

[英]Converting a python lookahead assertion regex to valid Golang

I have the following regex that works well in Python (due to lookahead assertions). 我有以下正则表达式在Python中效果很好(由于前瞻性断言)。

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

See example of myinput in the code block below. 请参见下面的代码块中的myinput示例。

Name: will always be the beginning of a group and USB Device Filters: will always be the end of a group. 名称:将始终是组的开始,而USB设备过滤器:将始终是组的结束。 Not all lines have a valid key:value, eg, can have or a blank line. 并非所有行都具有有效的key:value,例如可以具有或为空行。

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? 谁能帮我将其转换为有效的Golang正则表达式?

The ultimate goal is to parse myinput and get a slice of matched groups. 最终目标是解析myinput并获取匹配组的一部分。

Given this in Python: 鉴于此在Python中:

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

Demo 演示

Since ^(?=Name:) is a zero width assertion, Name: is captured by the capturing groups following it. 由于^(?=Name:)是零宽度的断言,因此Name:由其后的捕获组捕获。

You can capture the same in Golang with this: 您可以使用以下方法在Golang中捕获相同内容:

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

Demo 演示

If you don't want to capture Name:\\s you can do: 如果您不想捕获Name:\\s ,则可以执行以下操作:

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

Demo 演示

You don't need to escape the spaces with (?=USB\\ Device\\ Filters:) in either language. 您不需要使用两种语言中的(?=USB\\ Device\\ Filters:)来转义空格。 All have (?ms) flags set. 全部都设置了(?ms)标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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