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

2019-08-01 00:05发布

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++?

3条回答
We Are One
2楼-- · 2019-08-01 00:18

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++ :)

查看更多
放我归山
3楼-- · 2019-08-01 00:27

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
查看更多
男人必须洒脱
4楼-- · 2019-08-01 00:29

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)

查看更多
登录 后发表回答