posts - 28,  comments - 15,  trackbacks - 0

          概要

          什么是正則表達式,正則表達式用來干什么,可能會困擾一些初學者。我們都用過一些文本編輯器,例如 Word Word 為我們提供“查找—替換”功能,正則表達式就類似于 Word 的這種功能。 正則表達式 Regular Expression )就是一個字符構成的串,它定義了一個用來搜索匹配字符串的模式。

          Java.util. Pattern

          JDK 對正則表達式的支持,來源于 java.util.Pattern 類。在 java api 中定義 Pattern 為“ A

          compiled representation of a regular expression ”,譯為“正則表達式編譯后的表現”。

          Java.util.Matcher

          java api 中定義 Matcher 為“ An engine that performs match operations on a character sequence by interpreting a Pattern ”,
          可以譯為“依據解釋
          Pattern 而生成的字符序列來執行匹配操作的引擎”。 API 中,給出了典型的調用過程:

          ??????Pattern p = Pattern.compile("a*b");?? // 正則表達式

          ??????Matcher m = p.matcher("aaaaab");????? // 代匹配的字符串

          ??????boolean b = m.matches();

          正則表達式摘要

          字符集 (characters)

          字符

          含義

          x

          X 字符

          \\

          反斜線符號 (\)

          \0 n

          八進制數值 ( 其中 0<=n<=7)

          \0 nn

          八進制數值 ( 其中 0<=n<=7)

          \0 mnn

          八進制數值 ( 其中 0<=m<=3 0<=n<=7)

          \x hh

          十六進制數值

          \t

          Tab 字符 (‘ \u0009' ’)

          \e

          Esc 字符 (‘ \u001B' ’)

          \n

          換行符 (‘ \u000A' ’)

          \d

          [0-9]

          \D

          [^0-9]

          \w

          [A-Z0-9]

          ?

          ?

          ?

          ?

          字符類集

          字符

          含義

          [abc]

          a , b , or c (simple class)

          [^abc]

          Any character except a , b , or c ( )

          [a-zA-Z]

          a through z or A through Z , inclusive ( 區間 )

          [a-d[m-p]]

          [a-d[m-p]]

          a through d, or m through p: [a-dm-p] ( 聯合)

          [a-z&&[def]]

          d , e , or f ( 交集 )

          [a-z&&[^bc]]

          a through z , except for b and c : [ad-z] ( 子集 )

          [a-z&&[^m-p]]

          a through z , and not m through p : [a-lq-z] ( 子集 )


          其他的正則表達式請參考API。

          示例

          ??????private static final String procedureREx = "(REP_(\\d{1})_(\\d{1,2})(_(\\d{1}))?)";? //正則表達式
          ?????
          ???private static ProcedureInfo getProcedureInfo(String procedure) {
          ??????? ProcedureInfo info = new ProcedureInfo();

          ??????? String procedureName = null;
          ??????? String category = null;
          ??????? String reportNo = null;
          ??????? String rowType = null;

          ??????? Pattern procPattern = Pattern.compile(procedureREx);??????//編譯正則表達式

          ??????? Matcher matcher = procPattern.matcher(procedure);?????????//匹配字符串到正則表達式

          ??????? if (matcher.find()) {????????????????????????????????????//字符串是否匹配正則表達式
          ??????????? procedureName = matcher.group(1);
          ??????????? category = matcher.group(2);?????????????????????
          ??????????? reportNo = matcher.group(3);
          ??????????? rowType = matcher.group(5);

          ??????????? info.setName(procedureName);
          ??????????? info.setCategory(Integer.parseInt(category));
          ??????????? info.setReportNo(Integer.parseInt(reportNo));

          ??????????? if (rowType != null) {
          ??????????????? info.setRowType(Integer.parseInt(rowType));
          ??????????? }

          ??????????? info.setArgs(getArgs(procedure));
          ??????????? return info;
          ??????? } else {
          ??????????? System.out.println("can't recognize procedure:" + procedure);
          ??????????? return null;
          ??????? }
          ??? }

          參考:
          http://buy.ccw.cn/htm/app/aprog/01_7_31_4.asp
          http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/package-summary.html
          http://www.javaresearch.org/article/showarticle.jsp?column=331&thread=2488

          posted on 2006-04-25 17:43 zhangxl 閱讀(778) 評論(1)  編輯  收藏 所屬分類: JAVA 基礎文章


          FeedBack:
          #  JAVA 正則表達式
          2007-01-22 14:39 | fafdsa
          JAVA 正則表達式 JAVA 正則表達式 JAVA 正則表達式 JAVA 正則表達式  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類(17)

          隨筆檔案(28)

          文章分類(30)

          文章檔案(30)

          相冊

          收藏夾(2)

          hibernate

          java基礎

          mysql

          xml

          關注

          壓力測試

          算法

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 96750
          • 排名 - 600

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 历史| 乐清市| 奉节县| 南和县| 合江县| 庄浪县| 民勤县| 南通市| 冷水江市| 南皮县| 黔南| 水城县| 仲巴县| 白水县| 日土县| 延边| 库车县| 永善县| 德保县| 永丰县| 聊城市| 金门县| 衡水市| 察隅县| 韶山市| 洪洞县| 德昌县| 梨树县| 和政县| 永春县| 湄潭县| 昌图县| 义马市| 保山市| 冀州市| 萝北县| 长兴县| 四川省| 江永县| 广汉市| 重庆市|