0

I ma trying to lear Regex but when I ma following a tutorial the regex I wrote return None.

import re


pattern = 'lion'
string = 'Le lion est l\'animal national de l\'Inde'

mo = re.match(pattern, string)

print(mo)

#Print None


Why ? lion is present in my string

3
  • 2
    re.match requires that matches start at the beginning of the string. Use re.search instead. Commented Jun 11, 2020 at 8:59
  • 1
    You're missing a \ before the second d Commented Jun 11, 2020 at 9:06
  • @Nick Thank you for your quick and helpful replies, I figured it out that's why I suppress the post. Commented Jun 11, 2020 at 9:09

1 Answer 1

1

match requires that the string starts with the given pattern. I suspect you're looking for a function more like findall.

import re

pattern = r'lion'
string = 'Le lion est l\'animal national de l\'Inde'

mo = re.findall(pattern, string)

print(mo)

Output

['lion']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.