regular expression

点点圈 提交于 2021-02-16 21:08:56

问题


How do I write a regular expression to get that returns only the letters and numbers without the asterisks in between ?


回答1:


You could use a regex replacement here:

my $var = 'RMRIV43069411**2115.82';
$var =~ s/^.*?\D(\d+(?:\.\d+)*)$/$1/g;
print "$var";  // 2115.82

The idea is to capture the final number in the string, and then replace with only that captured quantity.

Here is an explanation of the pattern:

^                    from the start of the input
    .*?              consume all content up until
    \D               the first non digit character, which is followed by
    (\d+(?:\.\d+)*)  match AND capture: a number, with optional decimal component,
                     occurring before
$                    the end of the input

Then, we place with just this captured number, which is available in $1.



来源:https://stackoverflow.com/questions/66020676/regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!