[英]Regex to replace IP address
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
^([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])$
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.