<story><title>The Greatest Regex Trick Ever (2014)</title><url>http://rexegg.com/regex-best-trick.html</url></story><parent_chain><item><author>jandrese</author><text>The more general tip is that a single regex isn&amp;#x27;t the only tool you have.  You don&amp;#x27;t have to get your final product one one step.  Almost every &amp;quot;disaster&amp;quot; regex comes from someone trying to do too much at once.&lt;p&gt;One other solution would have been to run the regex twice, once to pick up all instances of Tarzan, and a second on the results of the first to filter out all instances of &amp;quot;Tarzan&amp;quot;.</text></item></parent_chain><comment><author>WJW</author><text>This is also often the problem with disaster SQL queries. I&amp;#x27;ve seen some monsters that got hopelessly tangled up in their own JOIN constraints, trying to fetch all the data in one roundtrip because OMG LATENCY but then having to do full table scans over large-ish tables instead. Rewriting it as three small indexed queries reduced the runtime from 40 minutes (!) to less than a second.&lt;p&gt;Don&amp;#x27;t do too much in one operation whether it&amp;#x27;s regexes, SQL queries or OOP classes!</text></comment>
<story><title>The Greatest Regex Trick Ever (2014)</title><url>http://rexegg.com/regex-best-trick.html</url></story><parent_chain><item><author>jandrese</author><text>The more general tip is that a single regex isn&amp;#x27;t the only tool you have.  You don&amp;#x27;t have to get your final product one one step.  Almost every &amp;quot;disaster&amp;quot; regex comes from someone trying to do too much at once.&lt;p&gt;One other solution would have been to run the regex twice, once to pick up all instances of Tarzan, and a second on the results of the first to filter out all instances of &amp;quot;Tarzan&amp;quot;.</text></item></parent_chain><comment><author>usrusr</author><text>A big source of trying to do too much is environments that offer easy regex-based  transformations defined as a pair of regex and a single replacement string (that may contain references to matching groups) and make other transformations hard (&amp;quot;while find + rest&amp;quot;). When you have the option to provide a &amp;quot;process match&amp;quot; closure instead of the replacement string the lure of putting too much into a single regex almost collapses.</text></comment>