[lug] regex question

Tkil tkil at scrye.com
Wed Jun 27 13:09:27 MDT 2001


>>>>> "charles" == charles  <charles at lunarmedia.net> writes:

charles> i'd like to test to see if a variable has two \@ symbols, and
charles> i am struggling with the best way to do so. i am sure the
charles> format of the string will be

um, if you really just want to see if it has two @ signs in it, you
can just count them:

  my $at_sign_count = $var =~ tr/@/@/;

which will be faster than any other regex type thing.

if you care that you have two valid e-mail addresses separated by one
or more spaces, that's a harder question.  you can make sure that the
two chunks have exactly one @ each by doing:

  my ($addr1, $addr2) = split $var;
  if ($addr1 && $addr2 &&
      $addr1 =~ /^[^@]+\@[^@]+$/ &&
      $addr2 =~ /^[^@]+\@[^@]+$/)
  {
    print "looking good!";
  }

determining if those e-mail addresses are actually "valid" or not is a
whole different question.

t.



More information about the LUG mailing list