[英]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". 然后,我想删除除“ anglais”和“ francais”之外的所有单词末尾的“ s”。
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 为了匹配任何词结尾的
s
,但anglais
和francais
,你可以使用
\b(?!(?:anglais|francais)\b)(\w*)s\b
See the regex demo 见正则表达式演示
Details 细节
\\b
- a leading word boundary \\b
前导词边界 (?!(?: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 (?!(?:anglais|francais)\\b)
-如果在当前位置的右边紧接有整个单词anglais
或francais
,则负向搜索将使匹配失败 (\\w*)
- Group 1: zero or more word chars (\\w*)
-第1组:零个或多个字字符 s
- an s
s
-an s
\\b
- a trailing word boundary. \\b
尾部单词边界。 In Java: 在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'. 您的评论听起来像是您只想找到以s结尾的单词。 If so, you could use something like this:
如果是这样,您可以使用以下方法:
[\\S]+(s\\b)
regexr link [\\S]+(s\\b)
regexr链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.