Reputation: 6076
I have a requirement where the string has to contain _exact
. I am using Java.
_en
or _ja
) at the end, add _exact before the locale._exact
is already present, don't add it again._exact
at the end of the string.E.g.:
something
-> something_exact
something_en
-> something_exact_en
something_ja
-> something_exact_ja
something_exact_en
-> something_exact_en
something_exact
-> something_exact
I spent some time and came up with 2 regex that, if ran in succession on the same string, make it possible. I am not sure if they cover all the possible cases though.
^(.*)(?<!_exact)(_(?:en|ja))$
^(.*)(?<!_exact)(?<!_(?:en|ja))$
If anybody could help me come up with just 1 regex that does the job, it would be great! Thank you!
Upvotes: 1
Views: 364
Reputation: 784998
You can use this regex:
str = str.replaceAll("^(?!.*_exact(?:_en|_ja)?$)(.+?)(_en|_ja)?$", "$1_exact$2");
(?!.*_exact(?:_en|_ja)?$)
is a negative lookahead that skips inputs that ends with _exact
or _exact_en
or _exact_ja
.Upvotes: 2