[Python-ideas] What about regexp string litterals : re".*" ?

Markus Meskanen markusmeskanen at gmail.com
Mon Mar 27 23:24:22 EDT 2017


On Mar 28, 2017 06:08, "Steven D'Aprano" <steve at pearwood.info> wrote:

On Mon, Mar 27, 2017 at 05:17:40PM +0200, Simon D. wrote:

> The regexp string litteral could be represented by : re""
>
> It would ease the use of regexps in Python, allowing to have some regexp
> litterals, like in Perl or JavaScript.
>
> We may end up with an integration like :
>
> >>> import re
> >>> if re".k" in 'ok':
>     ... print "ok"
>     ok

I dislike the suggested syntax re".k". It looks ugly and not different
enough from a raw string. I can easily see people accidentally writing:

    if r".k" in 'ok':
        ...

and wondering why their regex isn't working.


While I agree with most of your arguments, surely you must be the one
joking here? "Ugly" is obviously a matter of opinion, I personally find the
proposed syntax more beautiful than the // used in many other languages.
But claiming it's bad because people would mix it up with raw strings and
people not realizing is nonsense. Not only does it look very different, but
attempting to call match() or any other regex method on it would surely
give out a reasonable error:

  AttributeError: 'str' object has no attribute 'match'

Which _in the worst case scenario_ results into googling where the top
rated StackOverflow question clearly explains the difference between r''
and re''
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170328/27cdf085/attachment-0001.html>


More information about the Python-ideas mailing list