Home > other >  Language control regex for api [duplicate]
Language control regex for api [duplicate]

Time:10-09

I am looking for an regex to control language code for my localization service.

which need to control the format

en en-US

String regex1 = "^[a-z]{2}";
String regex2 = "^[a-z]{2}-[A-Z]{2}$";

I came up with these two, but I want to combine them .

How could I combine them?

Thanks.

CodePudding user response:

This is the regular expression that combines those 2. Making the first part mandatory, and the second part optional:

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

Demo: https://regex101.com/r/vRJEZm/1

  • Related