75.122.1.23
我需要将第三个数字替换为 2,使其看起来像 75.122.2.23
什么正则表达式可以匹配第三部分?
最佳答案
int replacementNumber = 2;
ip = ip.replaceAll("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})", "$1.$2."+replacementNumber+".$4");
评论表单:
/^ # match beginning
(
\d{1,3} # one address segment
\. # separation dot
\d{1,3}) # second address segment
\. # separation dot
\d{1,3} # address segment to be replaced
\. # separation dot
(\d{1,3}) # last address segment
$/x # match end, ignore comments
关于java - 正则表达式替换 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7101935/