我试图从字符串遵循此规则的末尾删除字母“s”:正则表达式不是取代所需的字符串
必须有一个元音某处字,是不是在第二次最后的位置。在字符串中的倒数第二个位置可以有一个元音,但如果只有元音处于倒数第二个位置,则不会删除“s”。
例如:
气体 - >不会得到改变,因为只有元音“a”是在位置上的“S”
玩具之前,右键 - >将成为“玩具”
例子 - >例子,因为在s之前的单词中有一个元音(e,a),而不仅仅是倒数第二个位置。
现在我有叫Tokenizer.java
public String remove(String word){
//replace any words that end with s if the preceding word part contains a vowel not immediately before the 's'
word.replaceAll("(?i)([aeiou][a-z]*[a-rt-z])s\\b", "$1");
return word;
}
类类中的方法,我调用此方法在我tester.java类 包标记生成器;
public class Tester {
public static void main(String[] args) {
String word = token.deleteS("causes");
System.out.println(word);
word = token.remove("examples");
System.out.println(word);
word = token.deleteS("toys");
System.out.println(word);
word = token.deleteS("gas");
System.out.println(word);
}
}
我的输出:
causes
examples
toys
gas
预期输出:
cause
example
toy
gas
我遇到的问题是,当我测试了这个regex 101 link,我看到它的工作。为什么它在java中的行为不同?
谢谢。对我来说非常愚蠢的监督。一直在这个太久。 – Anderology