regex implementation to replace group with its lowercase version

前端 未结 6 1250
南方客
南方客 2020-12-14 14:03

Is there any implementation of regex that allow to replace group in regex with lowercase version of it?

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 14:22

    In Perl, you can do:

    $string =~ s/(some_regex)/lc($1)/ge;
    

    The /e option causes the replacement expression to be interpreted as Perl code to be evaluated, whose return value is used as the final replacement value. lc($x) returns the lowercased version of $x. (Not sure but I assume lc() will handle international characters correctly in recent Perl versions.)

    /g means match globally. Omit the g if you only want a single replacement.

提交回复
热议问题