-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
I recently saw someone write re.sub('\\\$', '', s) when they could have just written s.replace('$', ''). Similarly, re.match("abc", s) could be replaced with s.startswith("abc"), re.search("abc", s) could be replaced with "abc" in s, re.fullmatch("abc", s) could be replaced with s == "abc", re.split("abc", s) could be replaced with s.split("abc").
Why is it better to use the str methods directly? It makes the code simpler and therefore easier to understand, may require less escaping, and it's probably usually faster.
Some thoughts on what the implementation could look like: We'd have to inspect the pattern string and ensure it doesn't contain any characters that are special for the regex engine, other than \ (for escaping) and in some cases ^ and $ for the beginning and end of the string. The implementation would also need to check whether any other flags are passed to the regex function (e.g., re.IGNORECASE would mean that a regex that contains alphabetical characters is no longer replaceable). There are other subtleties that I haven't fully thought through; for example, $ also matches right before a newline at the end of a string. An initial implementation could limit itself to simpler cases, and leave possible more complex cases for later.