[Python-ideas] Raw strings return compiled regexps

Gregory P. Smith greg at krypto.org
Sat Nov 10 08:04:40 CET 2007


Interesting idea.  Rather than breaking a lot of code you could have it be a
subclass of string that also adds the regular expression object methods.
Trivial to prototype such a type:

import re
class rstr(str):
  def __init__(self, x):
    str.__init__(self, x)
    self.__re = None
  def match(self, *args, **kwargs):
    if not self.__re:
      self.__re = re.compile(self)
    return self.__re.match(*args, **kwargs)
  def search(self, *args, **kwargs):
    if not self.__re:
      self.__re = re.compile(self)
    return self.__re.search(*args, **kwargs)
  def set_re_flags(self, flags):
    if self.__re:
      raise RuntimeError('flags may only be set once before the first use as
a regular expression.')
    self.__re = re.compile(self, flags)


Regardless, count me as +0 on the concept.  It seems neat but also smells
fishy.

-gps

On 11/9/07, Neil Toronto <ntoronto at cs.byu.edu> wrote:
>
> It seems like every time somebody has issues with raw strings, the
> canonical answer is "don't use them for that, use the for regular
> expressions".
>
> What if they just returned regular expression objects? As in
>
>    r'<some long exp>'.match('<my string>')
>
> That would guarantee they didn't get abused for anything else. It would
> break a lot of code, too. :)
>
> Quick question, if someone has the time: is there any way to test
> equivalence of regular expressions? If we had intersection and an
> emptiness test (both of which are easy in the theoretical construct, but
> harder to do in practice), it'd be easy. I may be able to fake
> intersection using lookahead and such, but there's no emptiness test
> that I know of.
>
> Thanks in advance,
> Neil
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20071109/19429631/attachment.html>


More information about the Python-ideas mailing list