Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to match data between the "@" and "."

@anylettersornumbers.

Is this a good way to do it???
$match =~ /@(\s+)\.)

Replies are listed 'Best First'.
Re: Pattern Matching help needed
by broquaint (Abbot) on Jun 27, 2003 at 14:04 UTC
    Use the \w character class and you should get the desired results e.g
    my $str = 'blah @letters12345andsoon. blah'; my($data) = $str =~ /@(\w+)\./; print "got - $data\n"; __output__ got - letters12345andsoon
    See. perlre for more info.
    HTH

    _________
    broquaint

Re: Pattern Matching help needed
by Abigail-II (Bishop) on Jun 27, 2003 at 14:05 UTC
    my ($found) = $match =~ /\@([^.]*)[.]/;

    Abigail

Re: Pattern Matching help needed
by neilwatson (Priest) on Jun 27, 2003 at 14:05 UTC
Re: Pattern Matching help needed
by cfreak (Chaplain) on Jun 27, 2003 at 14:09 UTC

    You're close. First of all you need to escape the '@' so it doesn't get interpreted. Secondly \s matches whitespace, not any character, \w is probably what you're looking for as it matches [A-Za-z0-9_]. And finally you need to end your regex with a /.

    $match =~ /\@(\w+)\./;

    Now even though you don't say it, it appears you're trying to match an email address, there are better ways to do it (check CPAN, I'm not certain of a specific module). But even your regex above can match on invalid addresses. Let me know if you're doing email and I'll show you a quick and dirty regex that can get most of them (though even its not perfect).

    Lobster Aliens Are attacking the world!
Re: Pattern Matching help needed
by tos (Deacon) on Jun 27, 2003 at 14:13 UTC
    if you are in doubt about whether a character is a metacharacter in a regex, there is no harm in tyring to escape the sign. Then f.i. you could do this with the '@'-sign as you did it with the dot.

    For letters and numbers you should use the \w-metacharacter. \s means all sorts of whitespaces.

    so

    $match =~ /\@(\w+)\./;
    should do the job.

    Apart from that is a view at man perlre highly recommended.

    greetings, tos

      Wow, Thanks for all the responses!