I want to do a match for a string when no abc
is followed by some characters (possibly none) and ends with .com
.
I tried with the following
Condensing:
Sorry if I did not make myself clear. Just give some examples.
I want def.edu, abc.com, abce.com, eabc.com and
abcAnYTHing.com do not match,
while a.com, b.com, ab.com, ae.com etc. match.
New regex after revised OP examples:
/^(?:(?!abc.*\.com\$|^def\.edu\$).)+\.(?:com|edu)\$/s
use strict;
use warnings;
my @samples = qw/
shouldn't_pass
def.edu abc.com abce.com eabc.com
should_pass.com
a.com b.com ab.com ae.com
abc.edu def.com defa.edu
/;
my $regex = qr
/
^ # Begin string
(?: # Group
(?! # Lookahead ASSERTION
abc.*\.com$ # At any character position, cannot have these in front of us.
| ^def\.edu$ # (or 'def.*\.edu$')
) # End ASSERTION
. # This character passes
)+ # End group, do 1 or more times
\. # End of string check,
(?:com|edu) # must be a '.com' or '.edu' (remove if not needed)
$ # End string
/sx;
print "\nmatch using /^(?:(?!abc.*\.com\$|^def\.edu\$).)+\.(?:com|edu)\$/s \n";
for my $str ( @samples )
{
if ( $str =~ // ) {
print "\n"; next;
}
if ( $str =~ /$regex/ ) {
printf ("passed - $str\n");
}
else {
printf ("failed - $str\n");
}
}
Output:
match using /^(?:(?!abc.*.com$|^def.edu$).)+.(?:com|edu)$/
s
failed - shouldn't_pass
failed - def.edu
failed - abc.com
failed - abce.com
failed - eabc.com
passed - should_pass.com
passed - a.com
passed - b.com
passed - ab.com
passed - ae.com
passed - abc.edu
passed - def.com
passed - defa.edu