我有一个String as Follows
String sentence = "I tried to speak @td Spanish @ty, and my @yd friend tried to @yi speak English @yy.";
我想用空格('')替换@td,@ if,@ yi等单词。 @xx字是动态的,在不同的场景中不断变化。
怎么做?
由于
答案 0 :(得分:2)
试试这个:
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String sentence = "I tried to speak @td Spanish @ty, and my @yd friend tried to @yi speak English @yy.";
sentence = sentence.replaceAll(" \\@[a-z]+", "");
System.out.println(sentence);
}
}
答案 1 :(得分:1)
试试这个:
sentence.replaceAll("@[A-za-z]+","");
正则表达式@[A-za-z]+
将查找仅由字母组成的所有单词,以@符号开头,并且至少包含1个字母。
或者,如果确保单词长度至少为2个字母,则可以使用正则表达式@[A-za-z]{2,}
,如果它们长度恰好为2个字母,则为@[A-za-z]{2}
。
答案 2 :(得分:0)