Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
No such thing as a small change
 
PerlMonks  

Problem with regex

by Anonymous Monk
on Feb 02, 2005 at 23:55 UTC ( [id://427520]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 00: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 01: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 05: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 08:20 UTC
    Sid,
    Might we have some output, such that we can help you more? Thanks.

    - Justin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://427520]
Approved by jbrugger
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.