正则表达式匹配 ResourceBundle
我需要一个与 ResourceBundle
的文件名匹配的正则表达式,该文件名遵循 name_lo_CA_le.properties
格式。它应该只匹配文件名中包含区域设置部分的包,并且名称部分不应包含下划线。
经过几个小时的实验,我得出了以下结论:
^[a-zA-Z]+(_([a-z]{2}(_[A-Z]{0,2})?|[a-z]{0,2}(_[A-Z]{2})?){1}(_\\w*)?){1}\\.properties$
它似乎并不适用于所有情况:
"bundle.properties".match(...); // false - correct
"bundle_.properties".match(...); // false - correct
"bundle_en.properties".match(...); // true - correct
"bundle__US.properties".match(...); // true - correct
"bundle_en_US.properties".match(...); // true - correct
"bundle_en__Windows.properties".match(...); // false!
"bundle__US_Windows.properties".match(...); // true - correct
"bundle_en_US_Windows.properties".match(...); // true - correct
我完全不知道如何从这里继续。以下是我对括号部分的推理:
(...){1}
与一个区域设置部分完全匹配。
(_([az]{2}(_[AZ]{0,2})?|[az]{0,2}(_[AZ]{2})?){1}
与两个字符的语言代码和可能为零且最多 2 个字符的国家/地区代码之一完全匹配,
(_\\w*)?
则相反。匹配一个变体或不匹配任何变体。
知道如何修复和/或改进此正则表达式吗?
I need a regular expression that will match the file name of a ResourceBundle
, which follows the format name_lo_CA_le.properties
. It should only match bundles that have a locale portion in their file names, and the name portion shall have no underscores.
After hours of experimentation I came up with the following:
^[a-zA-Z]+(_([a-z]{2}(_[A-Z]{0,2})?|[a-z]{0,2}(_[A-Z]{2})?){1}(_\\w*)?){1}\\.properties$
It doesn't seem to work for all cases:
"bundle.properties".match(...); // false - correct
"bundle_.properties".match(...); // false - correct
"bundle_en.properties".match(...); // true - correct
"bundle__US.properties".match(...); // true - correct
"bundle_en_US.properties".match(...); // true - correct
"bundle_en__Windows.properties".match(...); // false!
"bundle__US_Windows.properties".match(...); // true - correct
"bundle_en_US_Windows.properties".match(...); // true - correct
I have absolutely no idea how to proceed from here. Here's my reasoning behind the parenthesized part:
(...){1}
matches exactly one locale portion.
(_([a-z]{2}(_[A-Z]{0,2})?|[a-z]{0,2}(_[A-Z]{2})?){1}
matches exactly one of either a two-character language code and a possibly-zero-and-at-most-2-character country code or the other way around.
(_\\w*)?
matches one or no variant.
Any idea how to fix and/or improve this regular expression?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这与所有示例相匹配:
This matched all the examples:
你可以尝试这样的事情:
You can try something like:
这对我有用:
This works for me:
我最终使用的正则表达式:
它仍然与没有国家/地区的区域设置部分不匹配,如
bundle_en__Windows.properties
中,但它是我能想到的最好的。The Regex I ended up using:
It still doesn't match a locale portion with no country, as in
bundle_en__Windows.properties
, but it's the best I could come up with.