[Python-ideas] Give regex operations more sugar

Clint Hepner clint.hepner at gmail.com
Wed Jun 13 08:38:47 EDT 2018

> On 2018 Jun 13 , at 7:06 a, Ken Hilton <kenlhilton at gmail.com> wrote:
> Hi all,
> Regexes are really useful in many places, and to me it's sad to see the builtin "re" module having to resort to requiring a source string as an argument. It would be much more elegant to simply do "s.search(pattern)" than "re.search(pattern, s)".
> I suggest building all regex operations into the str class itself, as well as a new syntax for regular expressions.

I think you'll have to be more specific about why it is sad to pass strings to a function.

There already is a class that has all the methods you want, although with the roles of regex and string reversed
from what you want.

    >>> x = re.compile(r'[a-z]')  # x = !%a-z%!, or what have you
    >>> type(x)
    <type '_sre.SRE_Pattern'>
    >>> x.findall("1a3c5e7g9i")
    ['a', 'c', 'e', 'g', 'i']

Strictly speaking, a regular expression is just a string that encodes of a (non)deterministic finite automata.
A "regex" (the thing that supports all sorts of extensions that make the expression decidedly non-regular)
is a string that encodes ... some class of Turing machines.

[Aside: A discussion of just what they match can be found at
http://nikic.github.io/2012/06/15/The-true-power-of-regular-expressions.html, which suggests they can
match context-free languages, some (but possibly not all) context-sensitive languages, and maybe some languages
that context-sensitive grammars cannot not. Suffice it to say, they are powerful and complex.]

I don't think it is the job of the str class to build, store, and run the resulting machines, solely for the
sake of some perceived syntactic benefit.

I don't see any merit in adding regex literals beyond making Python look more like Perl.


More information about the Python-ideas mailing list