-2

I wrote:-

r = re.compile('|'.join([fr'\b[^-_]{email}\b' for email in self.member_email_domains()]), flags=re.I)

when i print r it gives re.compile('\\b[^-_]grad.edu\\b|\\b[^-_]m.grad.edu\\b', re.IGNORECASE) i want \b not \\b

i even used \\b in the place of \b but it did not help language used python

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

2 Answers2

-1

Printing a compiled regex shows you, in Python syntax, how to create an equivalent regex object. Note that the resulting expression does not use a raw string literal like your original input does, so it must escape the \b to match. In other words, the two are equivalent.

If you instead use print(r.pattern), that might clear up your confusion.

0x5453
  • 12,753
  • 1
  • 32
  • 61
-1

Thank you. Now i know what i was missing. but i still do not understand when i used this regex in regex101.com it matched my string grad.edu but in python code it keeps on going to else.findall returns an empty array

class Member:
    def member_email_domains(self):
      # ipdb.sset_trace()
      return ['grad.edu', 'm.grad.edu']
      # if self.email_domain:
      #           return self.email_domain
      # return [self.web_domain]

    def is_valid_email_for_member(self, email):
        email_domain = email.split('@')[-1]
        print(email_domain)
        r = re.compile('|'.join([fr'\b[^-_]{email}\b' for email in self.member_email_domains()]), flags=re.I)
        print(r.pattern)
        if re.search("^([a-zA-Z0-9][a-zA-Z0-9-_]*\.)*[a-zA-Z0-9]*[a-zA-Z0-9-_]*[[a-zA-Z0-9]+$", email_domain):
            if r.findall(email_domain):
                    print('true')
        return False

email='mahak.malik@grad.edu'