您的位置:首页 > 编程语言 > Java开发

深入入门正则表达式(java) - 3 - 正则在java中的使用

2012-09-08 11:17 519 查看
jdk版本选为1.6

1.5,1.4中的正则bug较多

我们先来总结一下java正则流派的特性,这里直接完全引用《精通正则表达式》中的表格

1.字符缩略表示法

\a [\b] \e \f \n \r \t \0octal \x## \u#### \cchar --- \u####只运行4位16进制数字;\0octal要求开头是0,后面接1至3为10进制数字;\cchar是区分大小写的,直接对后面字符的十进制编码进行异或操作。


2.字符组及相关结构

字符组:[...],[^...],可包含运算符

几乎任何字符:点号(根据模式不同,含义不同)

字符组缩略表示法:\w \d \s \W \D \S --- \w \W只能识别ASCII字符


3.锚点及其他零长断言

行/字符串起始位置:^ \A

行/字符串结束位置:$ \z \Z

当前匹配的起始位置:\G

单词分解符:\b \B --- 能够识别Unicode字符

环视结构:(?=...) (?!...) (?<=...) (?<!...) --- 顺序环视结构中可以使用任意正则表达式,逆序环视中只能匹配长度有限的文本


4.注释及修饰模式

模式修饰符:(?mods-mods)允许出现的模式:x d s m i u

模式修饰范围:(?mods-mods:...)

注释:从#到行末(只有在启动时有效) --- 只有在使用/x修饰符或者Pattern.COMMENTS选项时,#才算注释。没有转移的ASCII空白字符将被忽略。字符组内部的注释和空白字符也会被忽略


文字文本模式:\Q...\E

5.分组及捕获

捕获型括号:(...) \1 \2...

仅分组的括号:(?:...)

固化分组:(?>...)

多选结构:|

匹配优先量词:* + ? {n} {m,n} {m,}

忽略优先量词:*? +? ?? {n}? {n,}? {m,n}?

占有优先量词:*+ ++ ?+ {n}+ {n,}+ {m,n}+

ps:其中标注为蓝绿色的内容将在之后的教程讲解

下面开始介绍java中的正则api

首先看看正则的编译

Java代码







Pattern regex = Pattern.compile(".*?", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

Pattern regex = Pattern.compile(".*?", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

正则的编译相对来说很耗时 ,所以要注意复用。

第一个参数是正则,第二个是编译选项,可以同时指定多个,当然,也可以像下面这样什么也不指定

Java代码







Pattern regex = Pattern.compile(".*?");

Pattern regex = Pattern.compile(".*?");


Matcher


我们把字符串传给matcher,然后设置各种条件,最后再用它干活

下面看看matcher都能干些什么

首先要获取Matcher对象

Java代码







Matcher matcher = pattern.matcher(str);

Matcher matcher = pattern.matcher(str);

中途要更换正则

Java代码







matcher.usePattern(newPattern);

matcher.usePattern(newPattern);

中途替换目标字符串

Java代码







matcher.reset(str);

matcher.reset(str);

此时的matcher会丢失之前所有的明确的状态信息 - 比如下面要说到的搜索范围,之前匹配过的信息也就没有了

另一个相似函数,只是没有替换字符串而已

Java代码







matcher.reset();

matcher.reset();


设定搜索范围

Java代码







matcher.region(start, end); matcher.regionStart(); matcher.regionEnd();

matcher.region(start, end);
matcher.regionStart();
matcher.regionEnd();

第一个用做设置搜索边界。默认为搜索整个字符串

后两个用来得到设置的边界位置

设置边界后的环视

Java代码







matcher.useTransparentBounds(bool); matcher.hasTransparentBounds();

matcher.useTransparentBounds(bool);
matcher.hasTransparentBounds();

如果设置了边界,那么环视查找时,是否允许检查环视外的字符可以通过上面的函数设置

默认为false,也就是说不考虑边界之外的字符。

给一个简单的例子

目标字符串为abcde,我要查找b,但是要求b的前面是a。

如果边界设置为[1,5],也就是在bcde中查找,那么默认情况下是匹配不到结果的,,因为b已经在边界上了

但是如果允许在边界外检查,那么这里的b就符合要求

Java代码







String str = "abcde";
String regex = "(?<=a)b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
matcher.region(1, 5);//设置边界

System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看默认状态

System.out.println("find:" + matcher.find());//查找结果

matcher.reset();//重置

System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看重置后状态

matcher.useTransparentBounds(true);//设置

System.out.println("find:" + matcher.find());//查看结果

matcher.reset();//重置

System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看重置后状态

String str = "abcde";
String regex = "(?<=a)b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
matcher.region(1, 5);//设置边界
System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看默认状态
System.out.println("find:" + matcher.find());//查找结果

matcher.reset();//重置
System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看重置后状态
matcher.useTransparentBounds(true);//设置
System.out.println("find:" + matcher.find());//查看结果

matcher.reset();//重置
System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看重置后状态

输出:

Xml代码







hasTransparentBounds:false find:false hasTransparentBounds:false find:true hasTransparentBounds:true

hasTransparentBounds:false
find:false
hasTransparentBounds:false
find:true
hasTransparentBounds:true

我们可以看出,hasTransparentBounds默认是false

重置之后依然是false,当设置为true的时候再去重置,hasTransparentBounds没有改变

应用正则

查找

Java代码







matcher.find();
matcher.find(int);

matcher.find();
matcher.find(int);

find():在当前检索范围应用正则。如果找到匹配,返回true,否则返回false。多次调用,则每次都从上次匹配之后的位置开始查找。

Java代码







String str = "are you a boy?";
String regex = "\\b\\w+\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}

String str = "are you a boy?";
String regex = "\\b\\w+\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}

结果:

Xml代码







are you a boy

are
you
a
boy


find(int):参数为查找的起始偏移量。此函数不受当前检索范围影响,因为它调用了reset

Java代码







public boolean find(int start) {

int limit = getTextLength();
if ((start < 0) || (start > limit))
throw new IndexOutOfBoundsException("Illegal start index");

reset();
return search(start);
}

public boolean find(int start) {
int limit = getTextLength();
if ((start < 0) || (start > limit))
throw new IndexOutOfBoundsException("Illegal start index");
reset();
return search(start);
}


完全的匹配

Java代码







matcher.matches();

matcher.matches();

正则如果能完全匹配目标字符串,那么返回true,否则返回false。匹配成功意味着匹配的结果为检索范围开始到检索范围结束的所有文本。

Java代码







matcher.lookingAt();

matcher.lookingAt();

与matches()类似,但是不要求检索范围内的整段文本都能匹配

匹配结果

Java代码







matcher.group();
matcher.group(int);
matcher.groupCount();

matcher.group();
matcher.group(int);
matcher.groupCount();

group()返回上一次匹配的完整结果

group(int)返回上一次匹配中第N组的结果,如果N=0,那么同group()结果一样

Java代码







public String group() {
return group(0);
}

public String group() {
return group(0);
}

groupCount()返回捕获型括号的数目,组数

以下几个函数返回匹配结果的位置,其中无参的返回完整匹配的起始和结束位置,有参的返回分组匹配的起始和结束位置

Java代码







matcher.start();
matcher.start(int);
matcher.end();
matcher.end(int);

matcher.start();
matcher.start(int);
matcher.end();
matcher.end(int);

替换

Java代码







matcher.replaceAll(String); matcher.replaceFirst(String);

matcher.replaceAll(String);
matcher.replaceFirst(String);

返回目标字符串副本,其中匹配到的字符被替换

Java代码







matcher.appendReplacement(StringBuffer result, String replacement); matcher.appendTail(StringBuffer result);

matcher.appendReplacement(StringBuffer result, String replacement);
matcher.appendTail(StringBuffer result);

appendReplacement:将上次匹配结束到这次匹配之前的字符串加入result,然后将这次匹配的内容替换为replacement后加入result

appendTail:找到所有匹配(或用户期望的匹配)后,将剩余的字符串加入result

下面是jdk6中的示例

Java代码







Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());

Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());

输出:

Xml代码







one dog two dogs in the yard

one dog two dogs in the yard

红色为上次匹配之前到这次匹配之间的字符串

蓝绿色为这次匹配的字符串,将被替换成replacement

深蓝色为appendTail的工作

由于空格无法看出颜色,所以将空格用横线替代

过程为:

1."one- cat
-two-cats-in-the-yard",result="one-dog"

2."one- cat
-two- cat s-in-the-yard",result="one-dog-two-dogs"

3."one- cat
-two- cat
s
-in the yard
",result="one-dog two-dogs-in-the-yard"

扫描程序

两个相关的api

Java代码







matcher.hitEnd();
matcher.requireEnd();

/**
* Boolean indicating whether or not more input could change

* the results of the last match.

*
* If hitEnd is true, and a match was found, then more input

* might cause a different match to be found.

* If hitEnd is true and a match was not found, then more

* input could cause a match to be found.

* If hitEnd is false and a match was found, then more input

* will not change the match.

* If hitEnd is false and a match was not found, then more

* input will not cause a match to be found.

*/
boolean hitEnd;

/**
* Boolean indicating whether or not more input could change

* a positive match into a negative one.

*
* If requireEnd is true, and a match was found, then more

* input could cause the match to be lost.

* If requireEnd is false and a match was found, then more

* input might change the match but the match won't be lost.

* If a match was not found, then requireEnd has no meaning.

*/
boolean requireEnd;

matcher.hitEnd();
matcher.requireEnd();

/**
* Boolean indicating whether or not more input could change
* the results of the last match.
*
* If hitEnd is true, and a match was found, then more input
* might cause a different match to be found.
* If hitEnd is true and a match was not found, then more
* input could cause a match to be found.
* If hitEnd is false and a match was found, then more input
* will not change the match.
* If hitEnd is false and a match was not found, then more
* input will not cause a match to be found.
*/
boolean hitEnd;

/**
* Boolean indicating whether or not more input could change
* a positive match into a negative one.
*
* If requireEnd is true, and a match was found, then more
* input could cause the match to be lost.
* If requireEnd is false and a match was found, then more
* input might change the match but the match won't be lost.
* If a match was not found, then requireEnd has no meaning.
*/
boolean requireEnd;

hitEnd:

如果为true,继续输入可能导致之前的匹配更改为一个新的匹配
(或者之前匹配成功,之后丢失匹配,匹配失败**) ,或者之前没有匹配后来有了匹配。

如果为false,继续输入则不会改变匹配结果。

关于**说明:变量上面的注释似乎没有说明这一点,但是《精通正则表达式》提及到了,**的结论是正确的。下面给出一个例子

Java代码







String subjectString = "1";
Pattern regex = Pattern.compile("^\\d$", Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = regex.matcher(subjectString);
while(regexMatcher.find()){
System.out.println(regexMatcher.group());
System.out.println(regexMatcher.hitEnd());
}

String subjectString = "1";
Pattern regex = Pattern.compile("^\\d$", Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = regex.matcher(subjectString);
while(regexMatcher.find()){
System.out.println(regexMatcher.group());
System.out.println(regexMatcher.hitEnd());
}

上面的例子中,我只想匹配一个数字,那么结果是能匹配到的,输出如下

Xml代码







1 true

1
true


如果目标字符串有两个数字,那么

Java代码







String subjectString = "12";
Pattern regex = Pattern.compile("^\\d$", Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = regex.matcher(subjectString);
while(regexMatcher.find()){
System.out.println(regexMatcher.group());
System.out.println(regexMatcher.hitEnd());
}

String subjectString = "12";
Pattern regex = Pattern.compile("^\\d$", Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = regex.matcher(subjectString);
while(regexMatcher.find()){
System.out.println(regexMatcher.group());
System.out.println(regexMatcher.hitEnd());
}

则没有输出

也就是说,hitEnd=true,并且之前是能找到匹配的,但是继续输入字符串,结果有可能变为无法找到匹配。

requireEnd:

如果为true,继续输入可能导致之前的丢失之前的匹配结果

如果为false,并且找到了匹配,更多的输入可能会导致之前的匹配内容改变,但是结果不会改变;如果没有找到匹配,那么此变量无意义。

最后看看Pattern的几个方法

Java代码







split(CharSequence input);
split(CharSequence input,int limit);

split(CharSequence input);
split(CharSequence input,int limit);

split(CharSequence input):以input匹配到的内容做分割,返回分割好的数组

split(CharSequence input,int limit):分三种情况

1.limit<0:会保留结尾的空元素

2.limit=0:与split(CharSequence input)相同

3.limit>0:返回的数组最多为limit项,正则至多会应用limit-1次

下面对1和3举例说明:

Java代码







Pattern regex = Pattern.compile(",");
String[] ss = regex.split("a,b,c,d,",limit);
for (int i = 0; i < ss.length; i++) {

System.out.println(ss[i]);
}

Pattern regex = Pattern.compile(",");
String[] ss = regex.split("a,b,c,d,",limit);
for (int i = 0; i < ss.length; i++) {
System.out.println(ss[i]);
}

limit=-1时,数组为5个元素:“a”,“b”,“c”,“d”和一个空字符串

limit=2时,数组为2个元素:“a”,“b,c,d,”,只应用了一次正则

编译参数

Java代码







regex.flags();

regex.flags();

返回compile时传递的参数

块转义:

\Q...\E :将\Q和\E之间的正则转义为字面意义。 比如正则:\Q[1]\E,表示的是匹配一对方括号,里面有一个数字1,而不是只有数字1的字符组。

下面的静态函数有同样的功效

Java代码







regex.quote(String s);

regex.quote(String s);

例:

Java代码







System.out.println(Pattern.quote("[1]")); //输出为\Q[1]\E

System.out.println(Pattern.quote("[1]"));
//输出为\Q[1]\E


查找:

Java代码







Pattern.matches(String regex, CharSequence input);

Pattern.matches(String regex, CharSequence input);

看了matches的源码我们就知道其含义了

Java代码







public static boolean matches(String regex, CharSequence input) {

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}

public static boolean matches(String regex, CharSequence input) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}


至此java中的正则基本使用就介绍完了,希望大家拍砖的同时能给出意见,多谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: