Performing a regex substitution Perl

后端 未结 4 1013
春和景丽
春和景丽 2021-01-27 17:41

Assuming I have the IP 10.23.233.34 I would like to simply swap the 233 for 234. The first, second, and last octet are unknown. The thir

4条回答
  •  别那么骄傲
    2021-01-27 18:13

    This problem really isn't suited well for a regex solution. Instead, I would do something like the following:

    $in = "10.23.233.34";
    @a = split /\./, $in;
    if ($a[2] == '233') {
        $a[2] = '234';
    }
    print join(".", @a);
    

    The above code is far more readable than any regex you might come up with. Furthermore, the next person who has to maintain your code will be able to actually read it.

提交回复
热议问题