[英]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
。
.*
- 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: 做一个替换:
.*?(\\d{1,3}((.\\d{1,3}){3})).*
.*?(\\d{1,3}((.\\d{1,3}){3})).*
\\1
\\1
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
record marco
button (red button at the top) record marco
按钮(顶部的红色按钮) >
characters >
字符 End
button to move to the end of the next line End
按钮移动到下一行的末尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.