in reply to Counting characters within regexp

$em =~ /^(\w[\w|\.]).*@(\w\w).*\.(\w+)$/;

Note that in the [\w|\.] character class, the | (pipe) character matches a literal pipe character and is not an alternation. (I'm not sure if you intended a character match or not.) Also, escaping the . (period) character (\.) in the class is not needed, but does no harm.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Counting characters within regexp
by Bod (Parson) on May 09, 2021 at 09:26 UTC

    I used [\w|\.] to match either a an alphanumeric character or a real full stop - did I get that wrong?

    The beginning of \w[\w|\.] is to allow for cases where an email address consists of the person's initial followed by a full stop followed by their surname - e.g. j.bloggs@...

    EDIT - I guess I wanted (\w|\.) instead of [\w|\.]

      Here is how to use a character class to achieve that.

      use strict; use warnings; use Test::More; my @good = ( 'a', '9', '_', ); my @bad = ( '-', '$', '?', ); my $re = qr/[\w.]/; plan tests => @good + @bad; like $_, $re, "match for '$_'" for @good; unlike $_, $re, "no match for '$_'" for @bad;

      🦛