Re: Problem with ampersand and regex
by roboticus (Chancellor) on Dec 19, 2012 at 19:48 UTC
|
Try: my $line = 'mail: asdf@123.com';
...roboticus
When your only tool is a hammer, all problems look like your thumb.
| [reply] [d/l] |
Re: Problem with ampersand and regex
by toolic (Bishop) on Dec 19, 2012 at 19:50 UTC
|
my $line = 'mail: asdf@123.com';
| [reply] [d/l] |
Re: Problem with ampersand and regex
by davido (Cardinal) on Dec 19, 2012 at 19:51 UTC
|
use warnings;: "Possible unintended interpolation of @123..."
Use single quotes: my $line = 'mail: asdf@123.com'; and see if that clears it up for you.
use strict; doesn't catch this one because it treats "digit" variables special, and allows them without declaration.
| [reply] [d/l] [select] |
|
It looks like it trying to make the question overly simple, I made the mistake of omitting something very important. The definition of $line is coming from looping through an array as in the following:
foreach my $line (@array) {
. . .
}
With that in mind, how can I define $line with single quotes?
Sorry for the mistake.
| [reply] [d/l] |
|
| [reply] [d/l] |
|
|
Re: Problem with ampersand and regex
by frozenwithjoy (Priest) on Dec 19, 2012 at 20:39 UTC
|
Others have already answered your question, but I just wanted to point out something related to the title of your post since it could potentially cause confusion. An ampersand is '&' (the Perl sigil for subroutine), whereas '@' is called at sign, at symbol, apetail, commercial at, or even ampersat. | [reply] |
|
You are correct and thank you for pointing that out. Unfortunately, I am still having problems though. In an attempt to make the post easy to read, I left out some of the code and that makes the answer thus far impossible as far as I can see. Here is how my code should have looked:
foreach my $line (@array) {
if ($line =~ /mail: /) {
my $mail = substr($line, $+[$#+] );
print "mail=$mail\n";
}
}
With that, I see no way to use the single quotes as suggested. Any other possible solutions??
Thank you again. | [reply] [d/l] |
|
if( $line =~ /^mail:\s+([\S]+)$/ ) {
my $mail = $1;
print "mail=$mail\n";
}
| [reply] [d/l] |
|
|
|
|
$ cat t.pl
#!/usr/bin/perl
use strict; use warnings;
my @array = <DATA>;
foreach my $line (@array) {
if ($line =~ /mail: /) {
my $mail = substr($line, $+[$#+] );
print "mail=$mail\n";
}
}
__DATA__
This @ should be just fine
mail: this @ should also be fine
$ perl t.pl
mail=this @ should also be fine
...roboticus
When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] |
Re: Problem with ampersand and regex
by ikegami (Patriarch) on Dec 19, 2012 at 23:24 UTC
|
my $line = "mail: asdf@123.com";
should be
my $line = 'mail: asdf@123.com';
Then you can simply use
if (my ($mail) = $line =~ /mail: (.*)/) {
print "mail=$mail\n";
}
| [reply] [d/l] [select] |