我在正则表达式 (regex) 方面遇到了一个小问题。
我想删除字符串中每个单词末尾的任何“T”。
这是我用来显示所有以“T”结尾的单词的代码。
public static void main (String []args){
String name = "PHYLAURHEIMSMET hello tttttyyuolktttb fedqs jhgjt";
p = Pattern.compile("([a-z0-9]+)?[t]");
m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
}
感谢您的帮助。
最佳答案
i try to remove any "T" at the end of each word
string.replaceAll("T(?!\\S)", "");
这将匹配每个单词末尾出现的所有 T
。
或
string.replaceAll("T(?=\\s|$)", "");
仅当后面跟有空格或行尾 anchor 时,它才会匹配所有 t
。
进行不区分大小写的替换。
string.replaceAll("(?i)T(?!\\S)", "");
关于java - 正则表达式查找任何字符串的单词中的最后一个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30615525/