1-line conditional statement needs to be trimmed down I have this conditional statement with use of regex:

do_stuff if (@string.match(/&lt;(?:ingr?)&gt;/i) &amp;&amp; 
            @string.match(/&lt;(?:prod:alchemy?)&gt;/i)) ||
            @string.match(/&lt;(?:key?)&gt;/i)


It's a 1-line conditional statement, but due to it's length, it taking 3 lines instead. I'm wondering if there's a way to trim this down, so that I can fit it into 1 line. Preferably to be something like

do_stuff if (regex here)
You could combine all the tests into one regular expression, but I don't recommend it.  The following is equivalent to what you wrote:

do_stuff if @string =~ /(?'i'&lt;ingr?&gt;).*(?'a'&lt;prod:alchemy?&gt;)|\g&lt;a&gt;.*\g&lt;i&gt;|&lt;key?&gt;/i




Your regular expressions are a bit odd, so I suspect that what you wrote is not what you meant to write.

The non-capturing grouping parentheses (?:…) don't seem to serve any purpose, so you can get rid of them.

The other question marks make the preceding letter optional.  For example, /&lt;key?&gt;/i matches &lt;key&gt; or &lt;Key&gt; or &lt;ke&gt; but not &lt;ken&gt;.

The tricky obstacle to merging the regular expressions is that the &lt;ingr?&gt; and &lt;prod:alchemy?&gt; tests both have to pass, and they can occur in either order.  Therefore, you have to allow &lt;ingr?&gt;.*&lt;prod:alchemy?&gt; as well as &lt;prod:alchemy?&gt;.*&lt;ingr?&gt;  That's possible with

&lt;ingr?&gt;.*&lt;prod:alchemy?&gt;|&lt;prod:alchemy?&gt;.*&lt;ingr?&gt;


Of course, repeating yourself is bad for verbosity and maintainability.  You can lessen the pain slightly by using named groups and subexpression calls.

After having done all that, though, the one long regex is more difficult to understand than your original code.  Therefore, I recommend leaving it alone.

If you're dissatisfied with the original code, though, there may be other improvements that you could make, but you haven't provided any context for your question so I can't make alternative suggestions.