Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I want to know how to write a regex to first find a pattern in a line and then capture the value just next to it. example $line = "this is a test=234567 line"; Here i want to search for the pattern "test" and then get the value 234567 in a variable. I tried this ($value) = $line =~ /.*test=(\d+)/; but this does not seem to be working. Can any one explain how to get what i expect. Thanks in advance. regards, Sid

Replies are listed 'Best First'.
Re: Problem with regex
by pelagic (Priest) on Feb 03, 2005 at 05:19 UTC
    I don't know what your problem is. When I try:
    use strict; my $line = "this is a test=234567 line"; my ($value) = $line =~ /.*test=(\d+)/; print $value;
    I get nicely:
    234567

    pelagic
Re: Problem with regex
by chanakya (Friar) on Feb 03, 2005 at 06:30 UTC
    I got the correct value when I tried this

    use strict; my $line = "this is a test=234567 line"; if ($line =~ /.*test=(\d+)/){ print $1; }
    result
    234567
    Good Luck!!
Re: Problem with regex
by blazar (Canon) on Feb 03, 2005 at 10:28 UTC
    I tried this ($value) = $line =~ /.*test=(\d+)/; but this does not seem to be working.
    In which way does it "seem not to work"? As mentioned by two other monks, it should! As a side note /test=(\d+)/ should be perfectly equivalent to your code...
      Re: /.*test=(\d+)/ and /test=(\d+)/ ,of course, these are not entirely equivalent for other values of $line
        Now that I think about it you're right. Sorry for the imprecision...
Re: Problem with regex
by jpk236 (Monk) on Feb 03, 2005 at 13:20 UTC
    Sid,
    Might we have some output, such that we can help you more? Thanks.

    - Justin