Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Problem with ampersand and regex

by Anonymous Monk
on Dec 19, 2012 at 19:46 UTC ( #1009616=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code:
my $line = "mail: asdf@123.com"; if ($line =~ /mail: /) { my $mail = substr($line, $+[$#+] ); print "mail=$mail\n"; }
When ran, it prints mail=asdf.com. I'm trying to get asdf@123.com. I've tried several ways of quoting it but to no avail. Can someone give me a quick bit of assistance?

Thank you in advance.

Comment on Problem with ampersand and regex
Download Code
Re: Problem with ampersand and regex
by roboticus (Canon) 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.

Re: Problem with ampersand and regex
by toolic (Chancellor) on Dec 19, 2012 at 19:50 UTC
Re: Problem with ampersand and regex
by davido (Archbishop) 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.


    Dave

      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.

        How are you filling @array?


        Dave

Re: Problem with ampersand and regex
by frozenwithjoy (Curate) 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.
      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.

        What do you get if you capture?

        if( $line =~ /^mail:\s+([\S]+)$/ ) { my $mail = $1; print "mail=$mail\n"; }

        Dave

        The '@' isn't a problem when you read it from a file. Only if you're using it in a double-quoted string. For example:

        $ 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.

Re: Problem with ampersand and regex
by ikegami (Pope) on Dec 19, 2012 at 23:24 UTC

    Always use use strict; use warnings;!

    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"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009616]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2014-09-02 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls