我知道你已经接受了答案,但你的问题有另一个没有提及的简单解决方案。这种情况听起来与"regex-match a pattern, excluding..."的这个问题非常相似
关于使用正则表达式解析html的所有免责声明,这是一种简单的方法。
我们可以通过一个非常简单的正则表达式解决它:
]*>|( )
交替|的左侧匹配完成。我们将忽略这些匹配。右侧匹配并捕获第1组的空格,我们知道它们是正确的空格,因为它们与左侧的表达式不匹配。
这个完整的Java程序展示了如何使用正则表达式(参见online demo底部的结果):
import java.util.*;
import java.io.*;
import java.util.regex.*;
import java.util.List;
class Program {
public static void main (String[] args) throws java.lang.Exception {
String subject = "Hello!, Check out this 10 Movies Every Entrepreneur Needs to Watch ";
Pattern regex = Pattern.compile("]*>|( )");
Matcher m = regex.matcher(subject);
StringBuffer b= new StringBuffer();
while (m.find()) {
if(m.group(1) != null) m.appendReplacement(b, " ");
else m.appendReplacement(b, m.group(0));
}
m.appendTail(b);
String replaced = b.toString();
System.out.println(replaced);
} // end main
} // end Program
参考