zhyiwww
          用平實(shí)的筆,記錄編程路上的點(diǎn)點(diǎn)滴滴………
          posts - 536,comments - 394,trackbacks - 0
          一般的來(lái)說(shuō),使用正則表達(dá)式可以實(shí)現(xiàn)四個(gè)功能:匹配、分割、替換、刪除。
          嚴(yán)格的來(lái)說(shuō),其實(shí)替換和刪除是一個(gè)功能,不過(guò),從邏輯功能上來(lái)說(shuō),是不同的。
          下面,就是一個(gè)簡(jiǎn)單的demo也實(shí)現(xiàn)了上面的幾個(gè)功能,雖然比較簡(jiǎn)單,但是,對(duì)于不同的需求,都有對(duì)應(yīng)的實(shí)現(xiàn)。
          希望對(duì)初學(xué)的您也能提供一點(diǎn)思路。

          package org.zy.demo;

          import java.io.BufferedInputStream;
          import java.io.Reader;
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;

          /**
          ?* a testing demo of regular express
          ?* @author zy
          ?*
          ?*/
          public class RegularExpDemo {

          ?? ?public static void main(String[] args) {
          ?? ??? ?RegularExpDemo red = new RegularExpDemo();
          ?? ??? ?red.match();
          ?? ??? ?red.split();
          ?? ??? ?red.replace();
          ?? ??? ?red.delete();
          ?? ?}

          ?? ?/*
          ?? ? * just test match @param parent @param son
          ?? ? */
          ?? ?public void match() {
          ?? ??? ?String parent = "abcdefdssdefdsfed";
          ?? ??? ?String son = "d(ef)";
          ?? ??? ?Pattern p = Pattern.compile(son);
          ?? ??? ?Matcher m = p.matcher(parent);

          ?? ??? ?while (m.find()) {
          ?? ??? ??? ?System.out.println(" start? :? " + m.start());
          ?? ??? ??? ?System.out.println(" end : " + m.end());
          ?? ??? ?}

          ?? ?}

          ?? ?/*
          ?? ? * just test split
          ?? ? */
          ?? ?public void split() {
          ?? ??? ?String parent = "a/b/c/d/e/f/sd/ikshk/alke";
          ?? ??? ?String son = "/";
          ?? ??? ?Pattern p = Pattern.compile(son);

          ?? ??? ?String[] lstr = p.split(parent);
          ?? ??? ?int i = 0;
          ?? ??? ?while (i < lstr.length) {
          ?? ??? ??? ?System.out.println(lstr[i]);
          ?? ??? ??? ?i++;
          ?? ??? ?}

          ?? ?}

          ?? ?/*
          ?? ? * test replace
          ?? ? */
          ?? ?public void replace() {

          ?? ??? ?String parent = "abcdefdssdefdsfed";
          ?? ??? ?String son = "d[e|s].";
          ?? ??? ?Pattern p = Pattern.compile(son);
          ?? ??? ?Matcher m = p.matcher(parent);

          ?? ??? ?String lStr = m.replaceAll("ccgg");
          ?? ??? ?System.out.println(parent);
          ?? ??? ?System.out.println(lStr);

          ?? ?}

          ?? ?/*
          ?? ? * test delete
          ?? ? */
          ?? ?public void delete() {

          ?? ??? ?String parent = "abcdefdssdefdsfed";
          ?? ??? ?String son = "d[e|s].";
          ?? ??? ?Pattern p = Pattern.compile(son);
          ?? ??? ?Matcher m = p.matcher(parent);

          ?? ??? ?String lStr = m.replaceAll("");
          ?? ??? ?System.out.println(parent);
          ?? ??? ?System.out.println(lStr);

          ?? ?}
          }



          |----------------------------------------------------------------------------------------|
                                     版權(quán)聲明  版權(quán)所有 @zhyiwww
                      引用請(qǐng)注明來(lái)源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2007-09-19 17:40 zhyiwww 閱讀(902) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 正則表達(dá)式
          主站蜘蛛池模板: 鹤岗市| 梧州市| 湟中县| 安达市| 娱乐| 普格县| 黑河市| 贞丰县| 额济纳旗| 汉源县| 柳河县| 盱眙县| 济阳县| 仁化县| 荔浦县| 福泉市| 万盛区| 田阳县| 元朗区| 安宁市| 开江县| 寿光市| 丰镇市| 白朗县| 石棉县| 浦东新区| 墨江| 宁远县| 芮城县| 锦屏县| 五华县| 屏边| 泉州市| 石阡县| 河西区| 邹平县| 九龙城区| 沛县| 闸北区| 威远县| 江川县|