如何使用正则表达式删除仅包含单词的字符串?

时间:2015-05-14 16:50:11

标签: java regex string replaceall

1 line: "*hello:  "
2 line: "*hello:    "
3 line: "*hello: person answered"

想要完全删除第1行和第2行,但想要保留第3行,而不是它变为:

"*hello:person answered"

试过:

line = line.replaceAll("*hello:(\\s*)","");

1 个答案:

答案 0 :(得分:1)

你可以简单地做

line = line.replaceAll("^\\*hello:\\s*$","");

line = line.replaceAll("\\*hello:\\s*(?!.*\\w)","");

这应该为你做。参见演示。

https://regex101.com/r/mT0iE7/24