0

Why is re.search(".@.", email) returning valid for "a@bc" and "aa@b" ?

To my understanding "." is used to represent any single character except a newline. So given that when a user enters "a@bc" or "aa@b" I was expecting the program to return invalid since there are two characters before and after @.

Can you point me what is wrong with my above assumption ? Thanks in advance !

enter image description here

1 Answer 1

1

re.search() looks for any patterns that match your criteria and returns them in a "match object". So, when you define ".@." as your pattern, it will find a match anytime there is a '@' character with at least 1 character before and after. The match will be those 3 characters. Your examples both match this pattern, so you get a match object and the if test evaluates as True.
Run the code below to understand the behavior:

email = "ab@ac"
x = re.search(".@.", email)
print(x)
print(x.group(0))

Output is:

<_sre.SRE_Match object; span=(1, 4), match='b@a'>
b@a

Here are examples that will return no matching patterns for your search pattern. They return None, which will be False in your logic statement:

'my_name@'
'@domain.com'
1
  • Thanks for your answer @kcw78 Oct 30, 2022 at 4:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .