Correct syntax for pathological re.search()
Peter J. Holzer
hjp-python at hjp.at
Fri Oct 18 17:09:41 EDT 2024
On 2024-10-12 08:51:57 -0400, Thomas Passin via Python-list wrote:
> On 10/12/2024 6:59 AM, Peter J. Holzer via Python-list wrote:
> > On 2024-10-11 17:13:07 -0400, AVI GROSS via Python-list wrote:
> > > Is there some utility function out there that can be called to show what the
> > > regular expression you typed in will look like by the time it is ready to be
> > > used?
> >
> > I assume that by "ready to be used" you mean the compiled form?
> >
> > No, there doesn't seem to be a way to dump that. You can
> >
> > p = re.compile("\\\\sout{")
> > print(p.pattern)
> >
> > but that just prints the input string, which you could do without
> > compiling it first.
>
> It prints the escaped version,
Did you mean the *un*escaped version? Well, yeah, that's what print
does.
> so you can see if you escaped the string as you intended. In this
> case, the print will display '\\sout{'.
print("\\\\sout{")
will do the same.
It seems to me that for any string s which is a valid regular expression
(i.e. re.compile doesn't throw an exception)
assert re.compile(s).pattern == s
holds.
So it doesn't give you anything you didn't already know.
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.
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/20241018/caf4a15f/attachment.sig>
More information about the Python-list
mailing list