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
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
| [reply] [d/l] [select] |
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!! | [reply] [d/l] [select] |
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...
| [reply] [d/l] |
|
|
Re: /.*test=(\d+)/ and /test=(\d+)/
,of course, these are not entirely equivalent for other values of $line
| [reply] [d/l] [select] |
|
|
Now that I think about it you're right. Sorry for the imprecision...
| [reply] |
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 | [reply] |
|