4 ms·
speaking as an old regexp wizard from before perl5, this is indeed a great trick, have an upvote. sadly, this trick still requires a code comment to explain. P
by asah 5y ago
speaking as an old regexp wizard from before perl5, this is indeed a great trick, have an upvote.
sadly, this trick still requires a code comment to explain. Python example:
# match tarzan but not "tarzan"
# see https://news.ycombinator.com/item?id=27774584
if "tarzan" == re.search(r'"tarzan"|(tarzan)', myvar)[1]:
...
which in practice means it probably deserves a function:
if re_search_but_exclude(r'tarzan', myvar, '"tarzan"'):
...
I don't recommend monkeypatching re, i.e. re.search_but_exclude = ...
- dmurray 5y agoIs there a reason you have an r-string for the first arg but not for the third one?
- alisonkisk 5y agoIt's a harmless mistake.
- nytgop77 5y agoA bit off topic, but the commented version was much clearer, than the version with separate function. (full sentences are very good at explaining things)
- PennRobotics 5y agoIf you are comparing the match to the wanted string, it defeats the purpose of the capture group. if "tarzan" == re.search(r'"tarzan"|tarzan', myvar)[0]: ... Am I missing something?