Correct syntax for pathological re.search()

Peter J. Holzer hjp-python at hjp.at
Mon Oct 21 15:10:49 EDT 2024


On 2024-10-19 00:15:23 +0200, jak via Python-list wrote:
> Peter J. Holzer ha scritto:
> > As a trivial example, the regular expressions r"\\sout{" and r"\\sout\{"
> > are equivalent (the \ before the { is redundant). Yet
> > re.compile(s).pattern preserves the difference between the two strings.
> 
> Allow me to be fussy: r"\\sout{" and r"\\sout\{" are similar but not
> equivalent.

They are. Both will match the 6 character string 
    0005c \ REVERSE SOLIDUS
    00073 s LATIN SMALL LETTER S
    0006f o LATIN SMALL LETTER O
    00075 u LATIN SMALL LETTER U
    00074 t LATIN SMALL LETTER T
    0007b { LEFT CURLY BRACKET

> If you omit the backslash, the parser will have to determine if the
> graph is part of regular expression {n, m} and will take more time.

Yes, that's the parser. But the result of parsing will be the same:
The string will end in a literal backslash.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20241021/b0980859/attachment.sig>


More information about the Python-list mailing list