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 string
  • search() -> 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

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -