4 ms·
What about regular expressions that match the empty string? There might be ways the regular expression library could be designed better, but making re.match re
by yorwba 8y ago
What about regular expressions that match the empty string?
There might be ways the regular expression library could be designed better, but making re.match return a match when there's no match is not an improvement.
- PurpleRamen 8y agoNo match is also a match. The problem here is that it's bad design design if a function returns multiple different types of result. Generally there should only be one type of result to make handling reliable. It if's neccessary, you can still test for an empty result, making it the optional case that it is.
- est 8y agoand what's the use case for that? The main problem here is that you can't chain re.match().group() because None has no .group() Why do you want to match empty string with a group?
- yorwba 8y agoThe use case for a regular expression that matches the empty string is detecting whether something starts with an optional pattern. E.g. '(foo)?' to match the optional prefix 'foo'. FWIW if you really want re.match().group() to always return a string, you can just wrap all your regular expressions with '(...)?'. The reason why someone might want to be able to distinguish between "no match" and "the match is empty" is writing code that takes any arbitrary regular expression and uses it as a filter. Then someone else could pass in an expression that matches empty and everything works together nicely.