Search code examples
phpregexlocale

Avoid using capturing groups in locale regular expression test?


Regular expression only for checking only if the string is valid, not for capturing any part of it:

ISO 639-1 two-letter lowercase culture code and an ISO 3166-1 two-letter uppercase subculture code, or a ISO 639-1 lowercase culture code by itself.

That is it, it-IT for example. I ended up (I'm not so good in regular expression) with this:

^[a-z]{2}(-[A-Z]{2})?$

Using the capturing group (the 3166-1 part). Is the group really needed? How can avoid using it?


Solution

  • You need to use

    ^[a-z]{2}(?:-[A-Z]{2})?$
    

    To define non-capturing group you use (?:)