java - Best way to convert Locale properties names to normal names -
i reading list of file property file names bundle. need replace file contents in following format {en_gb==my name}
i using replaceall
method replace following characters "{en_gb" , "}"
can have number of =
symbols , string "en_"
may end gb or au
or us
etc.
what best way replace these?
string s = "{en_gb==kt!hibio}"; system.out.println(s.replaceall("[{\\w}]", ""));
you can use
string s = "{en_gb==pm!name}"; system.out.println(s.replaceall("\\{\\w+=+([^}]+)}", "$1"));
see ideone demo
the regex matches
\\{
- literal{
\\w+
- 1 or more alphanumerics or underscore=+
- 1 or more equal signs([^}]+)
- matches , captures group 1 one or more characters other}
}
- closing brace
when substring matched, matched text replaced text captured group 1 $1
.
your solution not work because regex have matching 1 character either {
or non-word character (\w
) , literal }
because enclosed expression sqaure brackets, forming character class.
now, since regex quite generic, may further narrow down matching capabilities this:
system.out.println(s.replaceall("(?i)\\{en_[a-z]+=+([^}]+)}", "$1"));
see another demo
this en_[a-z]+
required, , - (?i)
makes pattern case insensitive - match en_us
or en_us
.
in case know there 2 letters after en_
, use limiting quantifier {2}
:
system.out.println(s.replaceall("(?i)\\{en_[a-z]{2}=+([^}]+)}", "$1"));
Comments
Post a Comment