Skip to content
Advertisement

Regular expressions

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")

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement