java正则表达式使用示例
java正则表达式使用示例
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:packagecom.hongyuan.test;importjava.util.regex.Matcher;import...

复制代码 代码如下:

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));

}

}

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Java学习
热门Java学习
编程开发子分类