[Python-ideas] PEP 8: raw strings & regular expressions

Andrew Barnert abarnert at yahoo.com
Mon Oct 26 18:23:39 EDT 2015


On Oct 26, 2015, at 14:53, David Mertz <mertz at gnosis.cx> wrote:
> Obviously there can't be a regex to exclude everything that isn't a regex. Parentheses can nest to unlimited depths, so you need a formal grammar.
> 
As I said:
>> Obviously there's no actual regular expression that matches all regular expressions (you can't handle matched brackets without recursion or some other extension)

But you can do it trivially with Perl, or with the Regex module for Python, e.g., just by sticking a "(?1)" inside a pair of escaped parens plus a negative lookahead or nongreedy repetition. I'm not sure exactly how powerful Python (re module) regexes are (if I want to match something that isn't a regular language, I tend to reach for or build a dedicated parser rather than try to stretch re), but I know they're somewhere between actual regular expressions and perl regexes.
> But virtually everything that is a Windows path is also formally grammatical regex too (as are many things with no plausible likely intention as such)
> 
That's not true. You can, for example, have unclosed brackets or parens in a Windows path. And if you're wondering why anyone would do that, consider MP3 files auto-named based on their ID3v1/FreeDB metadata, which truncates fields at 29 or 30 bytes.

Anyway, as I said in the same message, it wouldn't be a useful heuristic because there's so much overlap, but you don't need to exaggerate that to make the same point.

>> On Oct 26, 2015 2:44 PM, "Ben Finney" <ben+python at benfinney.id.au> wrote:
>> Andrew Barnert via Python-ideas
>> <python-ideas at python.org> writes:
>> 
>> > Just for fun: is there a Python regex that matches all valid Python
>> > regexes?
>> 
>> Yes: ‘.*’ matches all valid Python regexes.
>> 
>> > Obviously there's no actual regular expression that matches all
>> > regular expressions (you can't handle matched brackets without
>> > recursion or some other extension).
>> 
>> You seem to be seeking something else: a pattern that matches all valid
>> regex patterns, *and* will never match any string that is not a valid
>> regex pattern. The latter is rather more difficult.
>> 
>> --
>>  \      “When I was born I was so surprised I couldn't talk for a year |
>>   `\                                        and a half.” —Gracie Allen |
>> _o__)                                                                  |
>> Ben Finney
>> 
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151026/8ed3e622/attachment.html>


More information about the Python-ideas mailing list