How can I match everything that is after the last occurrence of some char in a perl regular expression?

后端 未结 7 1328
误落风尘
误落风尘 2020-12-18 19:04

For example, return the part of the string that is after the last x in axxxghdfx445 (should return 445).

7条回答
  •  误落风尘
    2020-12-18 20:03

    my($substr) = $string =~ /.*x(.*)/;
    

    From perldoc perlre:

    By default, a quantified subpattern is "greedy", that is, it will match as many times as possible (given a particular starting location) while still allowing the rest of the pattern to match.

    That's why .*x will match up to the last occurence of x.

提交回复
热议问题