Regex - How to match multiline IP Address

Viewed 40

I have a file which contains data in key:value format. One of the key:value is IP Address: x.x.x.x. which can contain multiple IP addresses separated by newline. 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.

1 Answers

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.

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:

https://pkg.go.dev/regexp

https://gobyexample.com/regular-expressions

And for the syntax more is to be found here:

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

Related