Search and Replace - Remove specific IP ranges
-
Fellow Notepad++ Users,
Could you please help me the the following search-and-replace problem I am having?
I have rows of IP addresses with multiple IPs listed and I need to remove all entries in every row that begin with 172.* 128.* or fe*
Here is the data I currently have (“before” data):
172.16.0.1, 156.1.1.1, 127.0.0.1, fe20::250:90fd:fec5:7a2d, ::1 172.16.0.1, 172.1.1.1, 10.1.1.1, 127.0.0.1, fe20::250:90fd:fec5:7a2d, ffe20::250:90fd:fec5:7a2d, fe20::250:90fd:fec5:7a2d, ::1 172.15.0.1, 10.1.1.1, 127.0.0.1, fe20::250:90fd:fec5:7a2d, ::1 172.15.0.1, 10.1.1.1, 127.0.0.1, fe20::250:90fd:fec5:7a2d, ::1 172.12.0.1, 172.1.1.1, 10.1.1.1, 127.0.0.1, fe20::250:90fd:fec5:7a2d, ::1
Here is how I would like that data to look (“after” data):
156.1.1.1 10.1.1.1 10.1.1.1 10.1.1.1 10.1.1.1
Thanks for your time
-
What you say you want doesn’t quite match what you appear to want, based on your before and after.
In any case, this should work:
- go to the find/replace form (Ctrl+H assuming normal keybindings)
- set the
Search Mode
toRegular expression
Find what:
(?-i)(?:\b(?:(?:172|12[78])\.|ff?e)\S*,\x20*)|,\x20::1
Replace with: nothing (leave that box empty).You’ll have to hit the Replace All button twice.
This isn’t really a particularly clever or complex regular expression, although it may appear daunting. Some notes:
- the
(?-i)
makes the regex case-sensitive - the
(?:
and matching)
are just grouping parentheses that don’t capture - the
\b
matches a “word border” (that is, the border between a word character and a non-word character) \S*
matches zero or more non-whitespace characters\x20
matches a normal space character.