3 ms·
It 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 actua
by blhack 17y ago
It 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.
- CUViper 17y agoAha -- I almost suggested that they could do some memoization, and indeed they are doing just that. The last 100 unique patterns are compiled and stored in a dictionary for future use. You could still thrash badly if your regex "working set" is more than 100 patterns, but for most people it's probably Good Enough. (Plus you could override re._MAXCACHE if you really want.) Thanks for pointing out my error, and getting me to read the actual code instead of speculating. :)
- dfsdfsdsfdsf 17y agoMore likely: s = "something < other" s = s.replace("<", "<") Even more likely: import cgi s = cgi.escape(s) In Python, much string manipulation is done with builtins (especially string methods) and library functions. The latter work especially well when you target a specific domain (such as HTML) and will often be implemented in regexps. Writing one's own regexps are for when you actually need a small custom state machine. Many problems are too small or too large to justify the effort.