2

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}ロケール部分の1つに正確に一致します。

(_([a-z]{2}(_[A-Z]{0,2})?|[a-z]{0,2}(_[A-Z]{2})?){1}2文字の言語コードとおそらくゼロで最大2文字の国コードのいずれかまたはその逆のいずれかに正確に一致します。

(_\\w*)?1つのバリアントに一致するか、バリアントに一致しません。

この正規表現を修正および/または改善する方法はありますか?

4

4 に答える 4

2

これはすべての例と一致しました。

^[a-zA-Z\_\.]+[A-Z]{0,2}[a-zA-Z\_\.]*.properties$
于 2011-08-02T18:38:26.243 に答える
1

次のようなことを試すことができます:

^[a-zA-Z\_\.]+[A-Z]{2}[a-zA-Z\_\.]*.properties$
于 2011-02-20T04:50:04.543 に答える
0

これは私のために働きます:

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
  }
}
于 2011-02-20T05:32:25.890 に答える
0

私が使用することになった正規表現:

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

のように、国のないロケール部分とはまだ一致しませんbundle_en__Windows.propertiesが、私が思いつくことができる最高のものです。

于 2011-03-19T20:30:22.453 に答える