From what I figured,
match: given a string str and a pattern pat, match checks if str matches the pattern from str’s start.
search: given a string str and a pattern pat, search checks if str matches the pattern from every index of str.
If so, is there a meaning using '^' at the start of a regex with match?
From what I understood, since match already checks from the start, there isn’t. I’m probably wrong; where is my mistake?
When calling the function
re.matchspecifically, the^character does have little meaning because this function begins the matching process at the beginning of the line. However, it does have meaning for other functions in the re module, and when calling match on a compiled regular expression object.For example:
This prints:
With a
re.findall()andre.MULTILINEenabled, it gives you the first word (with no leading whitespace) on each line of your text.It might be useful if doing something more complex, like lexical analysis with regular expressions, and passing into the compiled regular expression a starting position in the text it should start matching at (which you can choose to be the ending position from the previous match). See the documentation for RegexObject.match method.
Simple lexer / scanner as an example:
which prints
This distinguishes between types of word; a word at the beginning of a line, a word at the end of a line, and any other word.