java正则表达式 java正则表达式使用示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java正则表达式 java正则表达式使用示例

  2021-03-19 我要评论
想了解java正则表达式使用示例的相关内容吗,在本文为您仔细讲解java正则表达式的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:正则表达式,下面大家一起来学习吧。

复制代码 代码如下:

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们