当前位置: 首页 > 学习 > 电脑学习 > 认证考试 > JAVA认证 > 考试资源 > 正文

在JAVA中使用正则表达式

http://www.zk168.com.cn  招考学习网 2006-4-4 20:02:53
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--


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

   < %@ 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, " cat ");

   }

   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
   {

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

   }

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

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

   for(i=0;i

   {

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

   }

   p = Pattern.compile(" +");

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

   for(i=0;i

   {

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

   }

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

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

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

   for(i=0;i

   {

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

   }

   %>

-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·实质、过程及意义——阿多尔诺“否定的辩证法”探微
·从Ontology的译名之争看哲学术语的翻译原则
·论马克思主义哲学经典的解释——解释学方法及其在马克
·中国哲学当前的核心与周边问题
·和合学与21世纪文化价值和科技
·中国文化的和合精神与21世纪
·宗教之间理当相互宽容
·上半个世纪的自由主义
·殷周至春秋时期神人关系之演进
·大学之道:构建以“三纲八目”为核心的道德修养体系
相关内容:
 
·环保企业人力资源开发与管理的实证研究————巨龙公
·重油制气污水处理系统(A/O)技术改造
·英美CPA管理模式及其启示
·改造NERA微波公务信道为国产监控信道
·EAStudio让电子商务网站如虎添翼
·基于PB6和ORACLE8开发“劳动信息管理系统”
·巧解Pretty  Park 病毒一例
·CDMA在中国的应用以及向CDMA2000的过度中的问题研究
·SMA施工控制与现场监理
·浅谈改性沥青及SMA路面平整度的控制
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>