我尝试过 restString = restString.replaceAll("\\<.*\\>", "");
和
restString = restString.replaceAll("\\<[^(\\>)]*\\>", "");
。
两者似乎都不起作用。不知道能不能表达出正则表达式的意思。
最佳答案
要使用正则表达式解决此问题,您需要“环顾四周” - 使用lookbehind和lookbefore:
restString = restString.replaceAll("(?<=<).*(?=>)", "");
lookaround 表达式将由正则表达式引擎计算,但它们不会成为匹配的一部分。 这样就可以删除 << 之间的所有内容。和>
如果它应该是非贪婪的并且能够匹配多行,请使用像 anubhava 建议的表达式:
restString = restString.replaceAll("(?s)(?<=<).*?(?=>)", "");
要了解有关环视的更多信息,请访问此页面:http://www.regular-expressions.info/lookaround.html
关于java - 删除字符串中 "<"和 ">"之间的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524947/