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. Eg:
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!
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.