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
This looks like an XY Problem.
DVK's answer shows you how you can tackle this problem using regular expressions, like you asked for.
My solution (in Python) demonstrates that regular expressions are not necessarily the best approach and that tackling the problem using your programming language's string-handling functionality may produce a more efficient and more maintainable solution.
#!/usr/bin/env python
import unittest
def is_valid_domain(domain):
return domain.endswith('.com') and 'abc' not in domain
class TestIsValidDomain(unittest.TestCase):
def test_edu_invalid(self):
self.assertFalse(is_valid_domain('def.edu'))
def test_abc_invalid(self):
self.assertFalse(is_valid_domain('abc.com'))
self.assertFalse(is_valid_domain('abce.com'))
self.assertFalse(is_valid_domain('abcAnYTHing.com'))
def test_dotcom_valid(self):
self.assertTrue(is_valid_domain('a.com'))
self.assertTrue(is_valid_domain('b.com'))
self.assertTrue(is_valid_domain('ab.com'))
self.assertTrue(is_valid_domain('ae.com'))
if __name__ == '__main__':
unittest.main()
See it run!
Update
Even in a language like Perl, where regular expressions are idiomatic, there's no reason to squash all of your logic into a single regex. A function like this would be far easier to maintain:
sub is_domain_valid {
my $domain = shift;
return $domain =~ /\.com$/ && $domain !~ /abc/;
}
(I'm not a Perl programmer, but this runs and gives the results that you desire)