Currently working on some RegExp to parse an input file for correct content. I'm using the below RegExp to parse some input:


Input it should match:


Or match this:


The -sit part of the input should be an optional capture group, which to my understanding means the RegExp will continue successfully if it does not find this capture group, or also finish successfully if it does find it.

For this instance, I would be using it in an if statement:

if [[ "$Input" =~ $RegExp ]];

Can anyone point out what is wrong with the above? I have been using regex101.com to test it.


bash understands standard extended regular expressions ("ERE"), not PCRE ("Perl-compatible regular expressions").

Your PCRE:


The (?:...) in a PCRE is a non-capturing group (not an optional group). There is no equivalent in an ERE and all groups are capturing.

To make an expression optional, you may qualify it with ?, as I have done below. The ? means that the previous expression should match one or zero times.

As an ERE:


or, contracting (SIT[a-z]|SIT[1-9]) into SIT[a-z1-9],


You may also want to add anchoring to this:


... otherwise it would match somethingcell-...-ablahblah

  • Hi there, thank you for your very in-depth explanation for me. I tried your simplified one and got 0 matches on both input strings, I noticed there was a missing capture group (that you may have gotten before my edit). To make it work I edited it to this: cell-(90|855|80|70)-(DEV|DEVL|SANDP|CAT|(SIT[a-z1-9]|SIT)|TAT|PROD)(-(DEV|DEVL|SANDP|CAT|(SIT[a-z1-9]|SIT)|TAT|PROD))?-[a-z] – 64Hz May 17 '18 at 13:46
  • @64Hz You may use SIT[a-z1-9]? to match all of SIT and SITa and SIT9. – Kusalananda May 17 '18 at 13:48
  • @64Hz Also note that case matters. – Kusalananda May 17 '18 at 13:52
  • I must have did something wrong the first time, you are 100% correct and it is working. Thank you very much. In regards to case, i will append a shopt -s nocasematch before any case sensitive steps. – 64Hz May 17 '18 at 13:53

(?:...) is not an optional capture group, but a non-capturing group, which - as far as I know - is not even supported by bash. This should work:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.