title: Look-ahead regular expression alternative
body: <p>I'm using ElasticSearch snapshot restore module and trying to get this to work:</p>
  <pre><code>  &quot;rename_pattern&quot;: &quot;^foo_(.+)(_bar)?$&quot;,
    &quot;rename_replacement&quot;: &quot;$1&quot;,
  </code></pre>
  <p>Meaning the string must start with foo_, followed by any number of characters, and it must end with _bar, which is optional. So both foo_some_string_bar and foo_some_string must end up as some_string. foo_some_string_bar_bar must end up as some_string_bar.</p>
  <p>Is this possible?</p>
  <p>Thanks.</p>
  <p>p.s: I managed to do it like this:</p>
  <pre><code>$ echo foo_some_string | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
  some_string
  $ echo foo_some_string_bar | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
  some_string
  $ echo foo_some_string_bar_bar | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
  some_string_bar
  $
  </code></pre>
  <p>It works, but it's sooo tricky and cumbersome.</p>