0

Hi does anyone know why this code returns a "No" and the other one returns a "Yes"?

import re
b="@gmail.com"
x=re.findall(r"\Bgmail",b)
if x:
    print("Yes")
else:
    print("No")  # <<<



import re
b="agmail.com"
x=re.findall(r"\Bgmail",b)
if x:
    print("Yes") # <<<
else:
    print("No")
azro
  • 53,056
  • 7
  • 34
  • 70

1 Answers1

4

As \b is word boundary, \B is the opposite


Your regex "\Bgmail" ask for :

  • gmail word
  • with NO word boundary before it
@gmail.com
^^ there is a word boundary between these 2 chars, so regex don't match

agmail.com
^^ there is NO word boundary between these 2 chars, so regex match

Regex Demo

Word boundaries doc

azro
  • 53,056
  • 7
  • 34
  • 70