Python, regex, and IRC

So, I’m on IRC a lot. I’m on a lot of channels, too. I’m on more than one Python channel. One scenario in these chans that comes up somewhat often is one in which a user converting from PHP, Perl, Ruby, or whatever walk in and want to get a better understanding of how regex works in Python.

Flaming ensues.

Flaming the flamers is a topic for another blog post, but for some reason, Python users seem to really be resistant to regex. At one point, I actually suggested in one of the larger channels that someone write an article for Python Magazine about the proper use (or non-use) of regular expressions. That was weeks ago now. I got no bites.

So here again, I’ll put this in a very public place and say that if you can get me a proposal for an article that details when to use or not use regex, and how to use them properly when you *should* use them, we will pay you to write that article.

  • eichin

    Speaking as someone who “ported” myself (and my employer) from perl to python a couple of years back… I think the big issue is baggage from other environments. For example, it took us a while to stop using

    if”\.jpg$”, line): …

    and switch to the more pythonic and readable

    if line.endswith(“.jpg”): …

    That’s just one example, but there are many other cases where you’ve got to step back and realize that the “natural” conversion of some regexp operation into python is a real native function, and not a regexp at all… and if the original perl RE is especially hairy, you owe it to yourself to (1) make it a function (2) give that function a docstring and some test cases!

    So, if I were to write such an article, it wouldn’t *be* a “regexp” article…