[New-bugs-announce] [issue16203] Proposal: add re.fullmatch() method

Guido van Rossum report at bugs.python.org
Thu Oct 11 23:30:42 CEST 2012


New submission from Guido van Rossum:

I've noticed a subtle bug in some of our internal code.  Someone wants to ensure that a certain string (e.g. a URL path) matches a certain pattern in its entirety.  They use re.match() with a regex ending in $.  Fine.  Now someone else comes along and modifies the pattern.  Somehow the $ gets lost, or the pattern develops a set of toplevel choices that don't all end in $.  And now things that have a valid *prefix* suddenly (and unintentionally) start matching.

I think this is a common enough issue and propose a new API: a fullmatch() function and method that work just like the existing match() function and method but also check that the whole input string matches.  This can be implemented slightly awkwardly as follows in user code:

def fullmatch(regex, input, flags=0):
  m = re.match(regex, input, flags)
  if m is not None and m.end() == len(input):
    return m
  return None

(The corresponding method will have to be somewhat more complex because the underlying match() method takes optional pos and endpos arguments.)

----------
keywords: easy
messages: 172695
nosy: gvanrossum
priority: normal
severity: normal
status: open
title: Proposal: add re.fullmatch() method
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16203>
_______________________________________


More information about the New-bugs-announce mailing list