JDK1.4及之后:

java.util.regex 是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包。

它包括两个类:Pattern 和 Matcher

Pattern :一个 Pattern 是一个正则表达式经编译后的表现模式

指定为字符串的正则表达式必须首先被编译为pattern类的实例。然后,可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。

public static Pattern compile(String regex)将给定的正则表达式编译并赋予给Pattern类

public static Pattern compile(String regex, int flags)同上,但增加flag参数的指定,可选的flag参数包括:CASE,INSENSITIVE,MULTILINE,DOTALL,UNICODE CASE, CANON EQ

[abc]a, b,

or c (simple class)

[^abc]Any character except a, b,

or c (negation)

[a-zA-Z]a through z or A through Z,

inclusive (range)

[a-d[m-p]]a through d,

or m through p: [a-dm-p] (union)

[a-z&&[def]]d, e,

or f (intersection)

[a-z&&[^bc]]a through z,

except for b and c: [ad-z] (subtraction)

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

and not m through p: [a-lq-z](subtraction)

.Any

character (may or may not match line terminators)

\dA

digit: [0-9]

\DA non-digit: [^0-9]

\sA

whitespace character: [ \t\n\x0B\f\r]

\SA

non-whitespace character: [^\s]

\wA word character: [a-zA-Z_0-9]

\WA

non-word character: [^\w]

Boundary

matchers

^The beginning of a line

$The

end of a line

\bA word boundary

\BA

non-word boundary

\AThe beginning of the input

\GThe

end of the previous match

\ZThe end of the input but for the final terminator,

if any

\zThe end of the input

Logical

operators

XYX followed by Y

X|YEither X or Y

(X)X,

as a capturing group

Pattern 的方法如下:

static Pattern

compile(String regex)

将给定的正则表达式编译并赋予给 Pattern 类

static Pattern

compile(String regex, int flags)

同上,但增加 flag 参数的指定,可选的flag参数包括:CASE INSENSITIVE, MULTILINE, DOTALL, UNICODE CASE, CANON EQ

int

flags()

返回当前 Pattern 的匹配 flag 参数

Matcher

matcher(CharSequence input)

生成一个给定命名的 Matcher 对象

static boolean

matches(String regex, CharSequence input)

编译给定的正则表达式并且对输入的字串以该正则表达式为模开展匹配,该方法适合于该正则表达式只会使用一次的情况,也就是只进行一次匹配工作,因为这种情况下并不需要生成一个 Matcher 实例

String

pattern()

返回该 Patter 对象所编译的正则表达式

String[]

split(CharSequence input)

将目标字符串按照 Pattern 里所包含的正则表达式为模进行分割

String[]

split(CharSequence input, int limit)

作用同上,增加参数 limit 目的在于要指定分割的段数,如将 limit 设为2,那么目标字符串将根据正则表达式分为割为两段

Matcher类:

Matcher 方法如下:

Matcher

appendReplacement(StringBuffer sb, String replacement)

将当前匹配子串替换为指定字符串,并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里

StringBuffer

appendTail(StringBuffer sb)

将最后一次匹配工作后剩余的字符串添加到一个StringBuffer对象里

int

end()

返回当前匹配的子串的最后一个字符在原目标字符串中的索引位置

int

end(int group)

返回与匹配模式里指定的组相匹配的子串最后一个字符的位置

boolean

find()

尝试在目标字符串里查找下一个匹配子串

boolean

find(int start)

重设 Matcher 对象,并且尝试在目标字符串里从指定的位置开始查找下一个匹配的子串

String

group()

返回当前查找而获得的与组匹配的所有子串内容

String

group(int group)

返回当前查找而获得的与指定的组匹配的子串内容

int

groupCount()

返回当前查找所获得的匹配组的数量

boolean

lookingAt()

检测目标字符串是否以匹配的子串起始

boolean

matches()

尝试对整个目标字符展开匹配检测,也就是只有整个目标字符串完全匹配时才返回真值

Pattern

pattern()

返回该 Matcher 对象的现有匹配模式,也就是对应的 Pattern 对象

String

replaceAll(String replacement)

将目标字符串里与既有模式相匹配的子串全部替换为指定的字符串

String

replaceFirst(String replacement)

将目标字符串里第一个与既有模式相匹配的子串替换为指定的字符串

Matcher

reset()

重设该 Matcher 对象

Matcher

reset(CharSequence input)

重设该 Matcher 对象并且指定一个新的目标字符串

int

start()

返回当前查找所获子串的开始字符在原目标字符串中的位置

int

start(int group)

返回当前查找所获得的和指定组匹配的子串的第一个字符在原目标字符

一个 Matcher 对象是由一个 Pattern 对象调用其 matcher() 方法而生成的,一旦该 Matcher 对象生成,它就可以进行三种不同的匹配查找操作:

① matches() 方法尝试对整个目标字符展开匹配检测,也就是只有整个目标字符串完全匹配时才返回真值

② lookingAt() 方法将检测目标字符串是否以匹配的子串起始

③ find() 方法尝试在目标字符串里查找下一个匹配子串

以上三个方法都将返回一个布尔值来表明成功与否

寻找匹配中的捕获组

public String group()

public String group(int group)

public String group(String name)

捕获组也就是Pattern中以括号对“()”分割出的子Pattern。至于为什么要用捕获组呢,主要是为了能找出在一次匹配中你更关心的部分。

捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 "(x)(y\\w*)(z)"中,存在三个这样的组:

1. x

2. y\\w*

3. z

组零始终代表整个表达式。

关于url、手机号、固定电话、银行卡号、传真号码、qq号、邮箱的正则表达式

static String urlRegex = "^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$"; // url匹配NEW

/**

* 验证手机号码

*

* 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147

* 联通号码段:130、131、132、136、185、186、145

* 电信号码段:133、153、180、189

*

*/

static String mobileRegex = "(\\+86|86)?((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$"; // 手机号码匹配

static String phoneRegex =

"^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";// 座机号码匹配

static String creditRegex = "([3-6]{1})([0-9]{15})([0-9]{3})";// 银行账号匹配

static String qqRegex = "[1-9][0-9]{4,9}$";// QQ号码匹配

static String faxRegex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$"; //传真匹配

static String emailRegex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";

使用案例:

Pattern p = Pattern.compile(mobileRegex);

Matcher m = p.matcher("13111311058");