4 ms·
Let's rewrite the examples using regular expressions (Python but that's not extremely important): import re re.match(r"^/(?P<year>.+?)/(?P<month>.+?)/(?P<
by cefstat 14y ago
Let's rewrite the examples using regular expressions (Python but that's not extremely important):
import re
re.match(r"^/(?P<year>.+?)/(?P<month>.+?)/(?P<day>.+?)/(?P<title>.+?)\.html$",
"/2012/08/12/test.html"
).groupdict()
re.match(r"^(?P<name>.+?) <(?P<email>.+?)> \((?P<url>.+?)\)$",
"John Doe <john@example.com> (http://example.com)"
).groupdict()
re.match(r"^from (?P<from>.+?) to (?P<to>.+?)$",
re.sub(r"\s+", " ", "from 4th October to 10th October")
).groupdict()
re.match(r"^convert (?P<quantity>.+?) (?P<from_unit>.+?) to (?P<to_unit>.+?)$",
"Convert 1500 Grams to Kilograms".lower()
).groupdict()
This didn't take long to write but even like this I would vastly prefer the proposed syntax of extract-values which I find much easier to remember and to write. So, yes, we can write regular expressions but why should we if for certain simple cases (and the rule is that most cases are simple) there is an easier way to do the same thing?
Think, for example, the built-in support for templates in Python (string.Template). Templates offer nothing that cannot not be done with the standard formatter but they are simpler to use. In a sense this proposal is the dual of templates, as it offers easy parsing for a restricted number of cases. And it definitely doesn't deserve all the negativity.
Having said that, I don't think I would use such facility unless it were part of the standard library. Writing a regular expression now and then is easier (at least for me) than having to keep around an extra library that, even with the help of pip and friends, I would have to re-install every time I change my environment (upgrade Python, setup a new machine etc.).
- secoif 14y agoYou can use tiny modules like this with near reckless abandon in node. Due to the way node's require() works and the way the package manager installs modules locally by default (rather than system-wide), most of the FUD of using 3rd party modules in other environments simply doesn't apply in node.
- mbreese 14y agoFor Python libraries, I used to shy away from adding extra modules, just because I didn't want to install things in the system Python site-packages. Then I started using virtualenv for everything, and my life has been much simpler. Now, I have a separate virtualenv setup for each project, and a simple script/Makefile to download and build dependencies. I also add a short shell script to wrap each project that automatically sets up the virtualenv environment and runs the appropriate Python script. Needless to say, I'm very happy that virtualenv itself has been added to the standard library.