在java中使用正则表达式_第1页
在java中使用正则表达式_第2页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、在 JAVA 中使用正则表达式jdk1 4 中加入了java util regex 包提供对正则表达式的支持。而且 Java lang String 类中的replaceAll 和split 函数也是调用的正则表达式来实现的。正则表达式对字符串的操作主要包括:字符串匹配,指定字符串替换,指定字符串查找和字符串分割。下面就用一个例子来说明这些操作是如何实现的: %Pattern p=null; /正则表达式Matcher m=null; /操作的字符串b;String s=null;StringBuffer sb=null;i=0;/字符串匹配,这是不符合的p = Pattern compile

2、(a*b);m = p matcher(baaaaab);b = m matches();out pr ln(b+);/字符串匹配,这是符合的p = Pattern compile(a*b);m = p matcher(aaaaab);b = m matches();out pr ln(b+);/字符串替换p = Pattern compile(ab);m = p matcher(aaaaab);s = m replaceAll(d);out pr ln(s+);p = Pattern compile(a*b);m = p matcher(aaaaab);s = m replaceAll(d)

3、;out pr ln(s+);p = Pattern compile(a*b);m = p matcher(caaaaab);s = m replaceAll(d);out pr ln(s+);/字符串查找p = Pattern compile(cat);m = p matcher(one cat two catshe yard);sb = new StringBuffer();while (m find() m appendReplacement(sb, dog);i+;m appendTail(sb);out pr ln(sb toString()+);out pr ln(i+);i=0;

4、p = Pattern compile(cat);m = p matcher(one cat two ca tshe yard);sb = new StringBuffer();while (m find() m appendReplacement(sb, dog);i+;m appendTail(sb);out pr ln(sb toString()+);out pr ln(i+);p = Pattern compile(cat);m = p matcher(one cat two catshe yard);p=m pattern();m = p matcher(bacatab);b = m

5、 matches();out pr ln(b+);s = m replaceAll(dog);out pr ln(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 pr ln(sb toString()+);out pr ln(i+);p = Pattern compile(cat);m = p mat

6、cher(one cat two catshe yard);sb = new StringBuffer();while (m find() m appendReplacement(sb, cat);m appendTail(sb);out pr ln(sb toString()+);String aa=sb toString();out pr ln(aa+);/字符串分割p = Pattern compile(a+);String a=p split(caaaaaat);for(i=0;iout pr ln(ai+);p = Pattern compile(a+);a=p split(c aa aaaa t,0);for(i=0;iA LENGTH;I+)out pr ln(ai+);p = Pattern compile( +);a=p split(c aa aaaa t,0);for(i=0;iA LENGTH;I+)out pr ln(ai+);p = Pattern compile(+);a=p split(dsaf

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论