I have recently been jumping into understanding regex with python.
I have been looking at the api; I can't seem to understand the difference between:
re.match vs. re.search
when is it best to use each of these? pros? cons?
Please and thank you.
I have recently been jumping into understanding regex with python.
I have been looking at the api; I can't seem to understand the difference between:
re.match vs. re.search
when is it best to use each of these? pros? cons?
Please and thank you.
re.match()
matches only from the beginning of the string. A common gotcha. See the documentation.
From Matching vs. Searching:
match
checks for a match only at the beginning of the string, while search
checks for a match anywhere in the string.
>>> re.match("c", "abcdef") # No match
>>> re.search("c", "abcdef") # Match
<_sre.SRE_Match object at ...>
I just learned you can also search for substrings this way:
if 'c' in 'abcdef'