Regex for Python 2.7

MRAB google at mrabarnett.plus.com
Sat Jan 24 15:23:51 EST 2009


Some time ago I discovered this difference between regular expressions
in Python and Perl:

Python

	\A matches at start of string
	\Z matches at end of string

Perl

	\A matches at start of string
	\Z matches before terminal newline or at end of string
	\z matches at end of string

In Perl \A == ^ and \Z == $ in single-string mode, but in Python \A == ^
and \Z != $ in single-string mode.

Obviously, changing Python's \Z to match Perl's could break existing
code. \z could be added, but with the opposite meaning to Perl's:

	Python's \Z == Perl's \z
	Python's \z == Perl's \Z

Would that be confusing?

Any suggests welcome.



More information about the Python-list mailing list