Regex to match ResourceBundle
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
开发者_Go百科
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?
This matched all the examples:
^[a-zA-Z\_\.]+[A-Z]{0,2}[a-zA-Z\_\.]*.properties$
You can try something like:
^[a-zA-Z\_\.]+[A-Z]{2}[a-zA-Z\_\.]*.properties$
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
}
}
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.
精彩评论