5 ms·
what do you hate about python regexes?
by llimllib 17y ago
what do you hate about python regexes?
- ghshephard 17y agoIt's difficult to explain what it is, precisely, about python RegExes that make them awkward, and perhaps if I had never encountered the syntatic regex sugar that is perl, I would have never noticed. But, as a total python advocate and frequent writer of python scripts - I still find my brain hitting wait-states that don't occur in perl when I need to pattern match. I can live with that because of the 50+ things that make python so much better (for me) than Perl (Hash of Array / Array of Hash at the top of the list) - but it's sad that I still don't regex in Python as fluidly as perl. I think the first hint that Python wasn't developed with RegEx users in mind would be that in the 700 Page "Learning Python, 3rd Edition" - Regular expressions aren't even mentioned in the Index.
- llimllib 17y agowell, I don't disagree that there's a tradeoff there - the s/ and =~ syntaxes are super neat, as opposed to the readable and consistent python style. I can see that as a reason for preferring perl's syntax, but I just don't understand hating python's regex, which is what my question was about.
- deleted 17y ago[deleted]
- blhack 17y agoIt probably has a lot to do with my lack of programming knowledge (I've never taken any formal programming classes [err...I took java in college but never actually went to the scheduled class because it was a lot of covering what these cRaZy things called variables were])... A replacement on python looks like this... foo = re.compile("<") bar = foo.replace(">", bar) this works, but it just feels less awkward to me than $bar = s/</>/
- llimllib 17y agohmmm, I would write the equivalent to your perl as: >>> re.sub("<", "<", "something < other") 'something < other' which seems much more readable and consistent to me than s/a/b, though the s/ syntax is great for vim users like me. I don't think this has as much to do with your programming knowledge as your python knowledge?
- granular 17y agoI disagree. Python regexen are a royal pain compared to how they work in Perl. You know what would be pretty funny is if a rogue group of guerilla programmers hacked Perl style regex match and replace syntax into Python. Heheheh, boy would that get some bees in some bonnets on the Python dev ML. :)
- ubernostrum 17y agoIt's been done, by Andrew Dalke, using a Python extension toolkit he developed: http://dalkescientific.com/Python/python4ply-tutorial.html#pattern http://dalkescientific.com/Python/python4ply-tutorial.html#p... He presented it at PyCon a couple years ago, response seemed to be "that's cool".
- CUViper 17y agoOne perl nicety is that the VM will optimize to only compile the expression once. Python's re.sub() has to compile the expression every time, since it's just a string argument rather than a language construct. You can manually cache the result of re.compile() -- it's just not as sugary that way.
- amix 17y agoThat's actually untrue. If you look in the implementation of the `re` module you would see that it caches the compilation of any regular expression.
- 17y ago