News for hackers. Stuff that matters.
Re:The Greatest Regex Trick Ever (2014) by asah on Thursday July 08, 2021 @03:22PM (#27776557)
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 = ...
Re:The Greatest Regex Trick Ever (2014) by PennRobotics on Friday July 09, 2021 @06:57AM (#27782395)
If 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?
Re:The Greatest Regex Trick Ever (2014) by dmurray on Thursday July 08, 2021 @03:48PM (#27776801)
Is there a reason you have an r-string for the first arg but not for the third one?
Re:The Greatest Regex Trick Ever (2014) by alisonkisk on Thursday July 08, 2021 @08:21PM (#27779217)
It's a harmless mistake.
Re:The Greatest Regex Trick Ever (2014) by nytgop77 on Thursday July 08, 2021 @06:08PM (#27778222)
A bit off topic, but the commented version was much clearer, than the version with separate function. (full sentences are very good at explaining things)