java正则篇

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。java正则篇,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

String html = "<tps>测试\n     </tps>";
String replacedHtml = html.replaceAll("\\s", "").replaceAll("&lt;tps&gt;(.*?)&lt;/tps&gt;", "<tps>$1</tps>");
System.out.println("Replaced HTML: " + replacedHtml);


如果cts标签外是span并且属性是color:red;font-size:15px则去掉cts标签
replaceAll("[\\r\\n]+", "")去掉换行
replaceAll("[\\r\\n]+| +","")去掉换行和空格
replaceAll("\\s", "")去掉换行空格

String html = "<div><span style='color:red;font-size:15px'><cts style='color:red;'>测试</cts>555</span></div>";
        Pattern pattern = Pattern.compile("<span style=['\"]color:red;font-size:15px['\"][^>]*><cts style='color:red;'>(.*?)</cts>(.*?)</span>");
Matcher matcher = pattern.matcher(html);

StringBuilder sb = new StringBuilder(html);
int start = 0;
while (matcher.find(start)) {
    // 获取匹配的内容并去除<cts>标签
    String contentWithoutCtsTag = matcher.group(1) + matcher.group(2);
    String spanWithContent = "<span style='color:red;font-size:15px;'>" + contentWithoutCtsTag + "</span>";

    sb.replace(matcher.start(), matcher.end(), spanWithContent);
    start = matcher.start() + spanWithContent.length();
}
System.out.println(sb.toString());


String str = "这是一个测试字符串\n";  
if (CharMatcher.whitespace().matchesAnyOf(str) || CharMatcher.is('\n').matchesAnyOf(str)) {  
        System.out.println("字符串中包含空格或换行符");  
    } else {  
        System.out.println("字符串中不包含空格或换行符");  
 }  

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/200897.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!