概要
什么是正則表達式,正則表達式用來干什么,可能會困擾一些初學者。我們都用過一些文本編輯器,例如
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 閱讀(763)
評論(1) 編輯 收藏 所屬分類:
JAVA 基礎文章