regular expression

Viewed 72

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

1 Answers

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.

Related