Differences between match and search in python regular expressions

Asked 2 years ago, Updated 2 years ago, 154 views

In Python, you try to find if the string contains "a something:" as a regular expression.

import re
re.match(r'a[\s\w]+:',"a something:")#a preceded by a space

This will result in None.

import re
re.search (r'a[\s\w]+:',"a something:")

If I do it, the results come out well. I know that there is a difference in the number of matches and search. Is there a difference in the search method?

regex python codesquad

2022-09-22 22:34

1 Answers

Match checks to see if it matches from the beginning, and search finds it everywhere. That's why I couldn't find you if there was a gap at the beginning.


2022-09-22 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.