隨筆-2  評(píng)論-16  文章-12  trackbacks-0

          <%@ page import="java.util.regex.*"%>
          <%
          Pattern p=null; //正則表達(dá)式
          Matcher m=null; //操作的字符串
          boolean b;
          String s=null;
          StringBuffer ***=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");
          *** = new StringBuffer();
          while (m.find()) {
          m.appendReplacement(***, "dog");
          i++;
          }
          m.appendTail(***);
          out.println(***.toString()+"<br>");
          out.println(i+"<br>");
          i=0;
          p = Pattern.compile("cat");
          m = p.matcher("one cat two ca tsi nthe yard");
          *** = new StringBuffer();
          while (m.find()) {
          m.appendReplacement(***, "dog");
          i++;
          }
          m.appendTail(***);
          out.println(***.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");
          *** = new StringBuffer();
          while (m.find()) {
          m.appendReplacement(***, "dog");
          i++;
          }

          m.appendTail(***);
          out.println(***.toString()+"<br>");
          out.println(i+"<br>");

          p = Pattern.compile("cat");
          m = p.matcher("one cat two cats in the yard");
          *** = new StringBuffer();
          while (m.find()) {
          m.appendReplacement(***, "<font color=\"red\">cat</font>");
          }
          m.appendTail(***);
          out.println(***.toString()+"<br>");
          String aa=***.toString();
          out.println(aa+"<br>");
          //字符串分割
          p = Pattern.compile("a+");
          String[] a=p.split("caaaaaat");
          for(i=0;i<a.length;i++)
          {
          out.println(a[i]+"<br>");
          }
          p = Pattern.compile("a+");
          a=p.split("c aa aaaa t",0);
          for(i=0;i<a.length;i++)
          {
          out.println(a[i]+"<br>");
          }
          p = Pattern.compile(" +");
          a=p.split("c aa aaaa t",0);
          for(i=0;i<a.length;i++)
          {
          out.println(a[i]+"<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[i]+"<br>");
          }
          %>

          posted on 2006-03-13 17:51 fadesea 閱讀(225) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 青海省| 渝中区| 兴国县| 宜城市| 楚雄市| 灵川县| 永昌县| 婺源县| 蒲江县| 寿光市| 黔南| 沭阳县| 东源县| 龙井市| 申扎县| 西乌珠穆沁旗| 安溪县| 普格县| 渝中区| 安仁县| 广汉市| 资兴市| 江阴市| 蛟河市| 五台县| 剑河县| 长宁县| 贺州市| 武陟县| 固原市| 赫章县| 福安市| 潼关县| 黑水县| 松江区| 新邵县| 高雄县| 错那县| 黎城县| 吉安市| 思南县|