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

How to delete a string only containing a word using regex?

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

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

"*hello:person answered"

尝试过:

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

你可以简单地做

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

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

这应该用于 you.See 演示。

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