如何使用正则表达式验证文化代码? [英] How can I validate a culture code with a regular expression?

查看:36
本文介绍了如何使用正则表达式验证文化代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不懂正则表达式,我也找不到任何正则表达式规则来验证文化代码:en-GB、en-UK、az-AZ-Cyrl 等.

I really don't understand regex and I also can't find any regex rule to validate culture codes as: en-GB, en-UK, az-AZ-Cyrl, others.

如何使用正则表达式验证这些代码?

How can I validate these codes with a regular expression?

推荐答案

你可以用这个来验证:

/^[a-z]{2,3}(?:-[A-Z]{2,3}(?:-[a-zA-Z]{4})?)?$/

这是它的工作原理

^       <- Starts with
[a-z]   <- From a to z (lower-case)
{2,3}   <- Repeated at least 2 times, at most 3
(?:     <- Non capturing group
   -        <- The "-" character
   [A-Z]     <- From a to z (upper-case)
   {2,3}     <- Repeated at least 2 times, at most 3
   (?:       <- Non capturing group
       -         <- The "-" character
       [a-zA-Z]  <- from a to Z (case insensitive)
       {4}      <- Repeated 4 times
   )         <- End of the group
   ?         <- Facultative
 )       <- End of the group
 ?       <- Facultative
 $       <- Ends here

如果只有 Cyrl 和 Latn 选项,您也可以用 (?:-(?:Cyrl|Latn))? 替换最后一个非捕获组

You can also replace the last non capturing group by (?:-(?:Cyrl|Latn))? if the only options are Cyrl and Latn

这篇关于如何使用正则表达式验证文化代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆