Swap multiple parts of an IP using a Perl REGEX [d

2019-09-21 17:05发布

问题:

Possible Duplicate:
Performing a regex substitution Perl

I have a line coming in via STDIN. I need to swap part of an IP address while retaining the line. IPs of the form XXX.XXX.233.XXX should change to XXX.XXX.234.XXX. Everything else must stay. For example the following line:

Hi My IP is 10.23.233.34. I live .233 miles from new york city in building 10.233 subsection .233.34. Ohh my friends IP is 10.33.233.55

should become

Hi My IP is 10.23.234.34. I live .233 miles from new york city in building 10.233 subsection .233.34. Ohh my friends IP is 10.33.234.55

I am not too good with Perl regex so maybe the Perl guys can chime in.

回答1:

s/(\d{1,3}\.\d{1,3})\.233(\.\d{1,3})/$1.234$2/

Will do what you want in PCRE