Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

regex extracting text

by sweepy838 (Acolyte)
on Apr 29, 2012 at 02:17 UTC ( #967882=perlquestion: print w/replies, xml ) Need Help??
sweepy838 has asked for the wisdom of the Perl Monks concerning the following question:

disregard this post, i was missing a "?" in (.*) .. problem solved :/
sub name($) { ($name)= $_[0] =~ m/\@(.*)\./; return $name; } print name('aaa@myname.whatever.com');
the output is myname.whatever but i just want "myname" can anyone explain why its jumping to the last dot?

Replies are listed 'Best First'.
Re: regex extracting text
by GrandFather (Sage) on Apr 29, 2012 at 04:17 UTC

    Often instead of using the non-greedy ? to modify .*, use a negated character class:

    sub name { my ($str) = @_; return $str =~ m/\@([^.]+)/ ? $1 : ''; }

    which makes the nature of the match clearer ("I want everything except '.'") and generally leads to fewer surprise matches.

    Note too that you should always use strictures (use strict; use warnings;) and (almost) never use prototypes. Assigning parameters to explicitly named variables helps make their use clearer, the nature of the parameters and use of the sub clearer, and avoids accidentally altering the variables passed into the sub (the elements in @_ are aliases to the parameters passed in).

    True laziness is hard work
      You subtly changed the OPs (.*) into a ([^.]+)? Did you do that on purpose? I believe so, otherwise, the line:
      return $str =~ m/\@([^.]+)/ ? $1 : '';
      doesn't make sense. The OP original patters allows for the capture of an empty string; and an undefined value will be returned if there was no match. If your pattern is changed to allow empty string matches, you can no longer distinguish between "not a match", and "empty match". You also leave off the matching of a dot, meaning you match on foo@bar, where the OP doesn't. Finally, there's no need to escape the @.

      sub name { return $1 if $_[0] =~ /@([^.*])\./; }
        thanks for that guys.. still learning regex :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967882]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
[PriNet]: quick question... anyone know how to remove all the existing keys in a %hash ? i've tried several options, but when i re-use the hash, some of the "upper" keys that i don't re-use stick and i can't get them to "remove"

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2017-06-28 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (619 votes). Check out past polls.