简体   繁体   中英

Java Regex: word ending in 's' except specific words

I am trying to find a regex. I have this:

String regex_words_ended_s_in_singular_form = "\\b(anglais|francais)\\b";

Then, I want to delete the 's' at the end of all words except for "anglais" and "francais".

I tried this, but it doesn't work of course:

String temp = tweet.replaceAll( "(?!" + regex_words_ended_s_in_singular_form + ")" + "s\\b","");

while (!temp.equals(tweet)) {
    tweet = temp;
    temp = tweet.replaceAll( "(?!" + regex_words_ended_s_in_singular_form + ")" + "s\\b","");
}
tweet = temp;

To match any word ending in s but anglais and francais you may use

\b(?!(?:anglais|francais)\b)(\w*)s\b

See the regex demo

Details

  • \\b - a leading word boundary
  • (?!(?:anglais|francais)\\b) - a negative lookahead that fails the match if there is a whole word anglais or francais immediately to the right of the current location
  • (\\w*) - Group 1: zero or more word chars
  • s - an s
  • \\b - a trailing word boundary.

In Java:

String res = s.replaceAll("\\b(?!(?:anglais|francais)\\b)(\\w*)s\\b", "$1");

Your comment makes it sound like you just want to find words ending in 's'. If so, you could use something like this:

[\\S]+(s\\b) regexr link

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM