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:<br><br>import re<br>class rstr(str):<br>
def __init__(self, x):<br> str.__init__(self, x)<br> self.__re = None<br> def match(self, *args, **kwargs):<br> if not self.__re:<br> self.__re = re.compile(self)<br> return self.__re.match(*args, **kwargs)
<br> def search(self, *args, **kwargs):<br> if not self.__re:<br> self.__re = re.compile(self)<br> return self.__re.search(*args, **kwargs)<br> def set_re_flags(self, flags):<br> if self.__re:<br> raise RuntimeError('flags may only be set once before the first use as a regular expression.')
<br> self.__re = re.compile(self, flags)<br><br><br>Regardless, count me as +0 on the concept. It seems neat but also smells fishy.<br><br>-gps<br><br><div><span class="gmail_quote">On 11/9/07, <b class="gmail_sendername">
Neil Toronto</b> <<a href="mailto:ntoronto@cs.byu.edu">ntoronto@cs.byu.edu</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
It seems like every time somebody has issues with raw strings, the<br>canonical answer is "don't use them for that, use the for regular<br>expressions".<br><br>What if they just returned regular expression objects? As in
<br><br> r'<some long exp>'.match('<my string>')<br><br>That would guarantee they didn't get abused for anything else. It would<br>break a lot of code, too. :)<br><br>Quick question, if someone has the time: is there any way to test
<br>equivalence of regular expressions? If we had intersection and an<br>emptiness test (both of which are easy in the theoretical construct, but<br>harder to do in practice), it'd be easy. I may be able to fake<br>intersection using lookahead and such, but there's no emptiness test
<br>that I know of.<br><br>Thanks in advance,<br>Neil<br>_______________________________________________<br>Python-ideas mailing list<br><a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/python-ideas">
http://mail.python.org/mailman/listinfo/python-ideas</a><br></blockquote></div><br>