Regex for Python 2.7

M

MRAB

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.
 
J

John Machin

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.

IIRC there was an exchange some years ago where the perl guy (Larry?)
admitted that he goofed and the consensus was that what Python had
done was better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top