python - Why use re.match(), when re.search() can do the same thing? -
from documentation, it's clear that:
match()
-> apply pattern match @ beginning of stringsearch()
-> search through string , return first match
and search
'^'
, without re.m
flag work same match
.
then why python have match()
? isn't redundant? there performance benefits keeping match()
in python?
"why" questions hard answer. matter of fact, define function re.match()
this:
def match(pattern, string, flags): return re.search(r"\a(?:" + pattern + ")", string, flags)
(because \a
matches @ start of string, regardless of re.m
flag status´).
so re.match
useful shortcut not strictly necessary. it's confusing java programmers have pattern.matches()
anchors search start and end of string (which more common use case anchoring start).
it's different match
, search
methods of regex objects, though, eric has pointed out.
Comments
Post a Comment