正则表达式匹配 ResourceBundle

发布于 2024-10-18 17:46:14 字数 1118 浏览 1 评论 0原文

我需要一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羁拥 2024-10-25 17:46:14

这与所有示例相匹配:

^[a-zA-Z\_\.]+[A-Z]{0,2}[a-zA-Z\_\.]*.properties$

This matched all the examples:

^[a-zA-Z\_\.]+[A-Z]{0,2}[a-zA-Z\_\.]*.properties$
叫思念不要吵 2024-10-25 17:46:14

你可以尝试这样的事情:

^[a-zA-Z\_\.]+[A-Z]{2}[a-zA-Z\_\.]*.properties$

You can try something like:

^[a-zA-Z\_\.]+[A-Z]{2}[a-zA-Z\_\.]*.properties$
水溶 2024-10-25 17:46:14

这对我有用:

public class Test {

  public static void main(String[] args) {
    String regex = "^[a-zA-Z]+(_)([a-z]{2})?(_)?([A-Z]{2})(_)?(\\w*)(\\.properties)$";

    assert "bundle.properties".matches(regex) == false;               // false - correct
    assert "bundle_.properties".matches(regex) == false;              // false - correct
    assert "bundle_en.properties".matches(regex) == false;            // false!
    assert "bundle__US.properties".matches(regex) == true;           // true - correct
    assert "bundle_en_US.properties".matches(regex) == true;         // true - correct
    assert "bundle_en__Windows".matches(regex) == false;             // false!
    assert "bundle__US_Windows.properties".matches(regex) == true;   // true - correct
    assert "bundle_en_US_Windows.properties".matches(regex) == true; // true - correct
  }
}

This works for me:

public class Test {

  public static void main(String[] args) {
    String regex = "^[a-zA-Z]+(_)([a-z]{2})?(_)?([A-Z]{2})(_)?(\\w*)(\\.properties)$";

    assert "bundle.properties".matches(regex) == false;               // false - correct
    assert "bundle_.properties".matches(regex) == false;              // false - correct
    assert "bundle_en.properties".matches(regex) == false;            // false!
    assert "bundle__US.properties".matches(regex) == true;           // true - correct
    assert "bundle_en_US.properties".matches(regex) == true;         // true - correct
    assert "bundle_en__Windows".matches(regex) == false;             // false!
    assert "bundle__US_Windows.properties".matches(regex) == true;   // true - correct
    assert "bundle_en_US_Windows.properties".matches(regex) == true; // true - correct
  }
}
仅冇旳回忆 2024-10-25 17:46:14

我最终使用的正则表达式:

^[a-zA-Z.]+(_([a-z]{2}(_[A-Z]{0,2})?|[a-z]{0,2}(_[A-Z]{2})?){1}(_\w*)?)\.properties$

它仍然与没有国家/地区的区域设置部分不匹配,如 bundle_en__Windows.properties 中,但它是我能想到的最好的。

The Regex I ended up using:

^[a-zA-Z.]+(_([a-z]{2}(_[A-Z]{0,2})?|[a-z]{0,2}(_[A-Z]{2})?){1}(_\w*)?)\.properties$

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文