java正則表達式使用示例
package com.hongyuan.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
public static void main(String[] args) {
String str="<html><head><title>regex test</title></head><body><p>this is a simle regex test</p></body></html>";
//拆分字符串
String[] splitStr=Pattern.compile("[</?|>]").split(str);
for(int i=0;i<splitStr.length;i++){
System.out.print(splitStr[i]+" ");
}
System.out.println();
//判斷字符串是否與制定模式匹配
boolean isMatching = Pattern.compile("^<(\\w*)>.*</\\1>$").matcher(str).matches();
System.out.println(isMatching);
//替換字符串
String repStr=Pattern.compile("<(/?)p>").matcher(str).replaceAll("<$1h1>");
System.out.println(repStr);
//提取字符串
Matcher m = Pattern.compile("<title>(.*)</title>").matcher(str);
while(m.find()){
System.out.println(m.group(1));
}
}
}
相關(guān)文章
使用Spring MVC實現(xiàn)雙向數(shù)據(jù)綁定
Spring MVC是一個廣泛用于構(gòu)建Java Web應(yīng)用程序的框架,它提供了眾多功能,包括雙向數(shù)據(jù)綁定,在這篇文章中,我們將向Java新手介紹如何使用Spring MVC實現(xiàn)雙向數(shù)據(jù)綁定,以及為什么這個特性如此重要,需要的朋友可以參考下2024-01-01IntelliJ IDEA編譯項目報錯 "xxx包不存在" 或 "找不到符號"
這篇文章主要介紹了IntelliJ IDEA編譯項目報錯 "xxx包不存在" 或 "找不到符號" ,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08Mybatis-config.xml中映射Mapper.xml文件遇到的錯誤及解決
這篇文章主要介紹了Mybatis-config.xml中映射Mapper.xml文件遇到的錯誤及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06