in reply to Reg ex question

say you want to match '99' in : 1990 , you can... $variable =~ /\d+(99)\d+/

will match the 99 in 1990, and store it in $1, because the regexp was put in the back reference using ()'s ie:

/\d+(\d+\d+)\d+/ will store the 2nd and 3rd digits in $1

$var =~ /hello\s+(.*)/ will store whatver is after hello in $1

if: $var = "hello world"; then $1="world";

Replies are listed 'Best First'.
Re: Re: Reg ex question
by larryk (Friar) on May 19, 2001 at 14:25 UTC
    /\d+(\d+\d+)\d+/ will store the 2nd and 3rd digits in $1 only on a 4 digit number. and if you are only dealing with 4 digit numbers then the regex can be more simply written as /\d(\d\d)\d/. the + sign refers to one or more occurrences of the preceding thing. what you will find is that if you have a number of more than 4 digits your regex will match the third-last and second-last. this is because perl's regexes are "greedy" so the first \d+ will suck up as many digits as it can before backtracking to allow the rest to match. again, this could be more simply written as /\d+(\d\d)\d/.

    larryk $less->{'chars'} = `"time in the pub" | more`; # :-D