简体   繁体   English

正则表达式 - 如何匹配多行 IP 地址

[英]Regex - How to match multiline IP Address

I have a file which contains data in key:value format.我有一个文件,其中包含键:值格式的数据。 One of the key:value is IP Address: xxxx which can contain multiple IP addresses separated by newline.其中一个键:值是 IP 地址:xxxx 可以包含多个 IP 地址,以换行符分隔。 Sample contents are below,示例内容如下,

======File contents===
key1: value1
IP Address: x.x.x.x
x.x.x.x
key2: value2
=====file end==========

I want to match the multiline IP address using regex.我想使用正则表达式匹配多行 IP 地址。

Have a look at the https://regex101.com/ website, there you can test your input and regex on the fly, it supports Golang and many more.看看https://regex101.com/网站,在那里你可以测试你的输入和正则表达式,它支持 Golang 等等。

If I assume your example looking like this:如果我假设您的示例如下所示:

======File contents===
key1: value1
IP Address: 10.0.0.1
192.168.0.1
key2: value2
key3: value3
=====file end==========

The most simple resulting regex would look like this:最简单的正则表达式如下所示:

([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)

In case you have more questions concerning Golang Regex-Package and how to compile the Regexp in use you'll find here all relevant information:如果您对 Golang Regex-Package 以及如何编译使用中的 Regexp 有更多疑问,您将在此处找到所有相关信息:

https://pkg.go.dev/regexp https://pkg.go.dev/regexp

https://gobyexample.com/regular-expressions https://gobyexample.com/regular-expressions

And for the syntax more is to be found here:更多的语法可以在这里找到:

https://github.com/google/re2/wiki/Syntax https://github.com/google/re2/wiki/Syntax

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

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