How to remove all ip in a text file with Notepad++

2019-07-31 23:56发布

问题:

I have a large text file containing words like this:

12.0.84.130|test|test|United States (US)
12.0.84.131
12.0.84.132

So i wanted to remove all the IP from that text file and make them appear like this:

12.0.84.130|test|test|United States (US)

How can i do this in Notepad++?

回答1:

You want to delete lines that contain only an IP address, here is how I'd do:

  • Ctrl+H
  • Find what: ^\d+(?:\.\d+){3}(?:\R|$)
  • Replace with: NOTHING
  • Replace all

Explanation:

^           : begining of line
\d+         : 1 or more digits
(?:         : start non capture group
  \.        : a dot
  \d+       : 1 or more digits
){3}        : end group, repeated 3 times
\R|$        : any kind of line break or end of line

If you want more accurate match for an iIP address, change \d+ by (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?1)){3} where (?1) is the same regex that the one in group 1.

  • Ctrl+H
  • Find what: ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?1)){3}(?:\R|$)
  • Replace with: NOTHING
  • Replace all


回答2:

In my experience the following works best to match and replace an exact IP address:

  • Ctrl+H
  • Find what: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
  • Replace with: NOTHING
  • Replace all

If you only want to remove IP's that contain new lines after them (Exactly like your before and after in the question) use:

(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([\r\n]+)

(Keep in mind when testing with your example 12.0.84.132 was left over because there is no newline after 12.0.84.132 when copying and pasting)



回答3:

Use Regex in Notepad++ Replace and search by Find what: (^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$) Replace: \1 |test|test|United States (US)

Dont forget to check Regular Expression in Notepad++ :)