简体   繁体   English

在Notepad ++中使用正则表达式进行IPv4地址搜索

[英]IPv4 address search using regular expression in Notepad++

I am trying to sanitize a configuration file in notepad++. 我试图在记事本++中清理配置文件。 Basically, I need to find and replace the IPv4 address with regular expression and replace only first two octet, I tried with following regular expression to search for IPV4, however didnt work in notepad++ 基本上,我需要查找并用正则表达式替换IPv4地址,并且仅替换前两个八位字节,我尝试使用以下正则表达式来搜索IPV4,但是在notepad ++中不起作用

\b(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(?1)){3}\b

Could anyone please help ? 有人可以帮忙吗?

Your regex \\b(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(.(?1)){3}\\b captures the first octet in group 1, then matches the 2nd and 3rd octet and captures the fourth octet in group 2. 您的正则表达式\\b(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(.(?1)){3}\\b捕获组1中的第一个八位位组,然后匹配第二个和第三个八位位组,并捕获组2中的第四个八位位组。

To replace the first 2 octets you want to match the first 2 octets and capture the last 2 octets to keep them in the replacement. 要替换前2个八位位组,您要匹配前2个八位位组并捕获后2个八位位组,以使其保持替换状态。

What you could do it to match all 4 octects using (?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d) for an octet and capture the last 2 including the leading dot for the third octet and the replace with XX followed by capturing group 1. 您可以使用(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)来匹配所有4个八位字节并捕获最后2个包括第三个八位位组的前导点,并用XX替换,然后捕获组1。

Find

\\b(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d))\\b

Replace with 用。。。来代替

XX\\1

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

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