75.122.1.23
我需要用2代替第三个数字,所以它看起来像75.122.2.23
正则表达式可以匹配第三部分?
答案 0 :(得分:4)
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
答案 1 :(得分:0)
^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.
([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$