在JAVA中使用正则表达式 (转)

本文介绍了Java中正则表达式的应用,包括字符串匹配、替换、查找及分割等操作,并通过具体实例展示了如何使用Pattern和Matcher类进行这些操作。
在JAVA中使用正则表达式 (转)[@more@] 

Java中使用正则表达式XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

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

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

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

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

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

  m = p.matcher("aaaaab");

  b = m.matches();

  out.println(b+"
");

//字符串替换

  p = Pattern.compile("ab");

  m = p.matcher("aaaaab");

  s = m.replaceAll("d"); 

  out.println(s+"
");

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

  m = p.matcher("aaaaab");

  s = m.replaceAll("d"); 

  out.println(s+"
");

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

  m = p.matcher("caaaaab");

  s = m.replaceAll("d"); 

  out.println(s+"
");

//字符串查找

 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()+"
");

 out.println(i+"
");

 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()+"
");

 out.println(i+"
");

 

 

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

 s = m.replaceAll("dog");

 out.println(s+"
");

 

 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()+"
");

 out.println(i+"
");

 

  p = Pattern.compile("cat");

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

  sb = new StringBuffer();

  while (m.find()) {

  m.appendReplacement(sb, "cat");

  }

m.appendTail(sb);

out.println(sb.toString()+"
");

String aa=sb.toString();

out.println(aa+"
");

//字符串分割

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

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

  for(i=0;i

  {

  out.println(a[i]+"
");

  }

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

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

  for(i=0;i

  {

  out.println(a[i]+"
");

  }

  p = Pattern.compile(" +");

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

  for(i=0;i

  {

  out.println(a[i]+"
");

  }

  p = Pattern.compile("+");

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

  out.println(a.length+"
");

  for(i=0;i

  {

  out.println(a[i]+"
");

  }

%>


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-962118/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-962118/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值