简体   繁体   English

Regex Notepad ++ Del除了IP之外的所有内容

[英]Regex Notepad++ Del all except IP

Have a text file, each line contains an IP address with useless info. 有一个文本文件,每行包含一个无用信息的IP地址。 Want to delete useless info around the IP. 想要删除IP周围无用的信息。

Each line looks like: 每行看起来像:

%BER-1-887373: Group <Connect> User <gacan> IP <72.34.184.200> No IPv6 address available

Need the IP Address only 仅需要IP地址

The < > surround the IP and need to delete those as well. <>包围IP,也需要删除它们。

Any help appreciated 任何帮助赞赏

Assuming you don't have any other groups of numbers separated by periods and enclosed in <...> , you can find: 假设您没有以句号分隔的任何其他数字组并用<...>括起来,您可以找到:

.*<(\d{1,3}(?:\.\d{1,3}){3})>.*

and replace with 并替换为

$1

This doesn't validate the IP address; 这不会验证IP地址; it just finds 4 groups of 1-3 numbers inside <...> and with periods between groups. 它只是在<...>找到4组1-3个数字,并且在组之间有句点。

Given your sample text, this would reduce this line 鉴于您的示例文本,这将减少此行

%BER-1-887373: Group <Connect> User <gacan> IP <72.34.184.200>

to just 72.34.184.200 . 72.34.184.200

Demo 演示

Explanation: 说明:

  • .* - match any character zero or more times (anything from the start of the line to the < ) .* - 匹配任何字符零次或多次(从行的开头到<任何字符)
  • < - match a literal character < < - 匹配文字字符<
  • ( - start a capturing group so we can use it in the replacement ( - 启动一个捕获组,以便我们可以在替换中使用它
    • \\d{1,3} - match 1 to 3 digits \\d{1,3} - 匹配1到3位数字
    • (?: - start a non-capturing group (?: - 开始一个非捕获组
      • \\. - match a literal . - 匹配文字.
      • \\d{1,3} - match 1-3 digits \\d{1,3} - 匹配1-3位数
    • ) - end the non-capturing group ) - 结束非捕获组
    • {3} - repeat the non-capturing group 3 times (to give us a total of 4 groups of digits, with 3 periods in between them) {3} - 重复非捕获组3次(总共给出4组数字,其间有3个句点)
  • ) - end the capturing group ) - 结束捕获组
  • > - match a literal > > - 匹配文字>
  • .* - match any character zero or more times (the rest of the line) .* - 匹配任何字符零次或多次(该行的其余部分)

The replacement $1 means to use the first capturing group (the part between ( and ) ), which in this case is the IP address. 替换$1表示使用第一个捕获组( ()之间的部分),在这种情况下是IP地址。 ( $0 would be all matched text, which in this case is the entire line). $0将是所有匹配的文本,在这种情况下是整行)。

Do a find replace: 做一个替换:

  • Find what: .*?(\\d{1,3}((.\\d{1,3}){3})).* 找到: .*?(\\d{1,3}((.\\d{1,3}){3})).*
  • Replace whith: \\1 替换whith: \\1
  • select Regular Expression 选择正则表达式
  • Hit Replace all 点击全部替换

The pattern is not perfect as it does not limit the range of the valid numbers (eg 999.999.999.99 will be matched too). 该模式并不完美,因为它不限制有效数字的范围(例如,999.999.999.99也将匹配)。

Regex tend to be complicated at times and there is a good chance you get it wrong. 正则表达式往往很复杂,很有可能你弄错了。 If all the lines are supposed to be in the specified format ie three words before IP and two words following it, I would use Macros. 如果所有的行都应该是指定的格式,即IP之前的三个单词和之后的两个单词,我会使用宏。 Let's say the example is: 让我们说这个例子是:

abc xyz xxxx <127.23.32.120> xxx xyz
  1. I would bring the cursor to the end of the first line. 我会把光标移到第一行的末尾。
  2. Press the record marco button (red button at the top) record marco按钮(顶部的红色按钮)
  3. Press Ctrl + Bksp twice. 按Ctrl + Bksp两次。 This will delete the xyz and xxx characters. 这将删除xyz和xxx字符。
  4. Press Bksp twice: This deletes the space and > characters 按两次Bksp:这将删除空格和>字符
  5. Press Ctrl + Left Arrow 7 times. 按Ctrl +向左箭头7次。 This will move the cursor at the beginning of the IP 这会将光标移动到IP的开头
  6. Press Ctrl + Bksp 4 times. 按Ctrl + Bksp 4次。 This will delete the other useless text and leave you just with the IP in the first line. 这将删除其他无用的文本,并在第一行留下您的IP。
  7. Press Down Arrow followed by the End button to move to the end of the next line 按向下箭头,然后按End按钮移动到下一行的末尾
  8. Stop recording the macro. 停止录制宏。
  9. Play the marco until the end of the file. 播放marco直到文件结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM