免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2550 | 回复: 1
打印 上一主题 下一主题

在JAVA中使用正则表达式[转载] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-09-04 10:12 |只看该作者 |倒序浏览
作者:leshui


jdk1.4中加入了java.util.regex包提供对正则表达式的支持。而且Java.lang.String类中的replaceAll和split函数也是调用的正则表达式来实现的。

  正则表达式对字符串的操作主要包括:字符串匹配,指定字符串替换,指定字符串查找和字符串分割。下面就用一个例子来说明这些操作是如何实现的:

<%@ page import="java.util.regex.*"%>;

<%

Pattern p=null; //正则表达式

Matcher m=null; //操作的字符串

boolean b;

String s=null;

StringBuffer sb=null;

int i=0;

//字符串匹配,这是不符合的

  p = Pattern.compile("a*b";

  m = p.matcher("baaaaab";

  b = m.matches();

out.println(b+"<br>;";

//字符串匹配,这是符合的

  p = Pattern.compile("a*b";

  m = p.matcher("aaaaab";

  b = m.matches();

  out.println(b+"<br>;";

//字符串替换

  p = Pattern.compile("ab";

  m = p.matcher("aaaaab";

  s = m.replaceAll("d";  

  out.println(s+"<br>;";

  p = Pattern.compile("a*b");

  m = p.matcher("aaaaab");

  s = m.replaceAll("d");  

  out.println(s+"<br>;");

  p = Pattern.compile("a*b");

  m = p.matcher("caaaaab");

  s = m.replaceAll("d");  

  out.println(s+"<br>;");

//字符串查找

p = Pattern.compile("cat");

m = p.matcher("one cat two cats in the yard");

sb = new StringBuffer();

while (m.find()) {

     m.appendReplacement(sb, "dog");

     i++;

}

m.appendTail(sb);

out.println(sb.toString()+"<br>;");

out.println(i+"<br>;");

i=0;

p = Pattern.compile("cat");

m = p.matcher("one cat two ca tsi nthe yard");

  sb = new StringBuffer();

while (m.find()) {

     m.appendReplacement(sb, "dog");

     i++;

}

m.appendTail(sb);

out.println(sb.toString()+"<br>;");

out.println(i+"<br>;");





p = Pattern.compile("cat");

m = p.matcher("one cat two cats in the yard");

p=m.pattern();

m = p.matcher("bacatab");

b = m.matches();

out.println(b+"<br>;");

s = m.replaceAll("dog");

out.println(s+"<br>;");



i=0;

p = Pattern.compile("(fds){2,}");

m = p.matcher("dsa da fdsfds aaafdsafds aaf");

  sb = new StringBuffer();

while (m.find()) {

     m.appendReplacement(sb, "dog");

     i++;

}

m.appendTail(sb);

out.println(sb.toString()+"<br>;");

out.println(i+"<br>;");



  p = Pattern.compile("cat");

  m = p.matcher("one cat two cats in the yard");

  sb = new StringBuffer();

  while (m.find()) {

     m.appendReplacement(sb, "<font color=\"red\">;cat</font>;");

  }

m.appendTail(sb);

out.println(sb.toString()+"<br>;");

String aa=sb.toString();

out.println(aa+"<br>;");

//字符串分割

  p = Pattern.compile("a+");

  String[] a=p.split("caaaaaat");

  for(i=0;i<a.length;i++)

  {

  out.println(a+"<br>;");

  }

  p = Pattern.compile("a+");

  a=p.split("c aa aaaa t",0);

  for(i=0;i<a.length;i++)

  {

  out.println(a+"<br>;");

  }

  p = Pattern.compile(" +");

  a=p.split("c aa    aaaa t",0);

  for(i=0;i<a.length;i++)

  {

  out.println(a+"<br>;");

  }

  p = Pattern.compile("\\+");

  a=p.split("dsafasdfdsafsda+dsagfasdfa+sdafds");

  out.println(a.length+"<br>;");

  for(i=0;i<a.length;i++)

  {

  out.println(a+"<br>;");

  }

%>;

论坛徽章:
0
2 [报告]
发表于 2003-09-08 15:55 |只看该作者

在JAVA中使用正则表达式[转载]

bang
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP