COOLak
COOLak

Reputation: 399

Notepad++ RegEx: match IP address ranges that correspond to x.x.x.x/24

I have a huge list of IP ranges, where each range is split with a hyphen and placed on the new line, and I only need to keep the IPs within the same 3-level range: 50.197.62.0-50.197.62.255, but not 50.197.62.0-50.197.73.125, etc. So far couldn't figure how to do this. I would really appreciate any help here. Thanks.

Additional examples.

Would like to keep:
4.49.119.0-4.49.119.255
12.161.203.64-12.161.203.79
23.25.83.201-23.25.83.201
24.23.113.0-24.23.113.255
38.101.137.0-38.101.137.255
38.107.144.0-38.107.144.255
38.107.146.0-38.107.146.255
50.73.187.0-50.73.187.216

Would like to match with a regex so I can remove them:
64.58.246.0-64.58.247.255
66.212.138.0-66.212.140.255
67.172.21.0-67.172.23.255
67.186.41.128-67.186.50.115
68.235.192.0-68.235.223.255
70.89.104.0-70.89.105.87
71.16.71.96-71.16.72.103
71.60.156.12-71.60.162.255
71.162.15.128-71.162.12.98
72.22.23.0-72.22.24.127

Upvotes: 0

Views: 2311

Answers (2)

dmitry1100
dmitry1100

Reputation: 1329

You can manage it with BAT command instead of RegEx

for /F "tokens=1-8 delims=.-" %a in (iplist.txt) do @if %c EQU %g echo %a.%b.%c.%d-%e.%f.%g.%h>> newiplist.txt

Or if you want first 3 blocks to be equal:

for /F "tokens=1-8 delims=.-" %a in (iplist.txt) do @if "%a.%b.%c" EQU "%e.%f.%g" echo %a.%b.%c.%d-%e.%f.%g.%h>> newiplist.txt

Assuming that iplist.txt is all IPs list and newiplist.txt is new IPs list.

I'd like to notice that you should use single % if run right from shell and double % (%%a, %%b etc) if create batch file.

Upvotes: 1

melwil
melwil

Reputation: 2553

If you mean that you wish to match any IP address that starts with "50.197.62" you could do this:

/50\.197\.62\.\d{1,3}/

Usually specifying ranges in IP addresses are done like this: 50.197.62.0/24

It's called CIDR notation, and you can read more about that here: https://networkengineering.stackexchange.com/questions/3697/the-slash-after-an-ip-address-cidr-notation

EDIT:

After understanding that you wished to match the entire string "ip-ip", it got a lot clearer.

/(\d{1,3}\.\d{1,3}\.\d{1,3}\.)\d{1,3}-\1\d{1,3}/g

Demo: https://regex101.com/r/cO1nQ6/1

EDIT 2:

Reverse:

/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)\d{1,3}-(?!\1)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/gm

This needs the flag m for multiline to force the search to go from start of line. I should have used that on the other as well.

Demo: https://regex101.com/r/eQ5uF1/3

Upvotes: 1

Related Questions