首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java正则表达式学习(1)

2012-09-14 
Java正则表达式学习(一)1.什么是正则表达式:正则表达式(regular expressions) 是一种描述字符串集的方法,

Java正则表达式学习(一)

1.什么是正则表达式:


正则表达式(regular expressions) 是一种描述字符串集的方法,它是以字符串集中各种字符串的公有特征为依据的。

?

正则表达式可以用于搜索、编辑或者操作文本和数据。许多程序设计语言都支持利用正则表达式进行字符串操作。

?

正则表达式这个概念最初是由Unix中工具软件(例如sed和grep)普及开来的。正则表达式通常缩写成"regex"。

?

2.java.util.regex包是如何描述正则表达式的?


java.util.regex 包主要由三个类所组成:Parten、Matcher和PatternSyntaxException。


Pattern 对象表示一个已编译的正则表达式。Patter类没有提供公有的构造方法。要构造一个模型,首先必须调用公共的静态compile方法,它将返回一个Pattern对象。这个方法接受正则表达式作为第一个参数。


Matcher 是一个靠着输入的字符串来解析这个模式和完成匹配操作的对象。与Pattern相似,Matcher也没有定义公共的构造方法,需要通过调用Pattern对象的matcher方法来获得一个Matcher对象。


PatternSyntaxException 对象是一个未检查异常,指示了正则表达式中的一个语法错误。

?

?

测试正则表达式工具,java版

在后面的实例中,可通过该工具来验证

?

?

package com.fortune.test;import java.io.Console;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created with IntelliJ IDEA. * User: Alan * Date: 12-5-28 * Time: 下午1:46 */public class RegexTestHarness {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while (true) {            System.out.printf("%nEnter your regex: ");            Pattern pattern = Pattern.compile(scanner.nextLine());            System.out.printf("Enter input string to search: ");            Matcher matcher = pattern.matcher(scanner.nextLine());            boolean found = false;            while (matcher.find()) {                System.out.printf(                        "I found the text "%s" starting at index %d and ending at index %d.%n",                        matcher.group(), matcher.start(), matcher.end()                );                found = true;            }            if (!found) {                System.out.printf("No match found.%n");            }        }    }}

?


3.字符串验证


在大多数的情况下,API所支持模式匹配的基本形式是匹配字符串,如果正则表达式是foo,输入的字符串也是foo,这个匹配将会是成功的。因为这两个字符串是相同的。试着用测试工具来测试一下:


?

Enter your regex: fooEnter input string to search: fooI found the text "foo" starting at index 0 and ending at index 3.
?

?

?

结果确实是成功的。注意当输入的字符串是3个字符长度的时候,开始的索引时0,结束的索引是3。这个是约定俗成的,范围包括开始的索引,不包括结束的索引,如下图所示:



Java正则表达式学习(1)
?图1 ?字符串"foo"的单元格编号和索引值


字符串中每一个字符位于其自身的单元格(cell)中,在每一单元格之间有索引指示位。字符串"foo"始于索引0处,止于索引3处,即使是这些字符它们自己仅占据了0、1和2号单元格。


就子序列匹配而言,你会注意到一些重叠,下一次匹配开始索引与前一次匹配的结果索引是相同的:

?

?

Enter your regex: fooEnter input string to search: foofoofooI found the text "foo" starting at index 0 and ending at index 3.I found the text "foo" starting at index 3 and ending at index 6.I found the text "foo" starting at index 6 and ending at index 9.

?


4.元字符验证?


API也支持许多可以影响模式匹配的特殊字符。把正则表达式改成 cat. ?, 并输入字符串"cats",输出结果是下所示:


?

Enter your regex: cat.Enter input string to search: catsI found the text "cats" starting at index 0 and ending at index 4.
?

?

?

虽然在输入的字符串中没有点(.),但这个匹配仍然是成功的。这是由于点(.)是一个元字符(metacharacters)(被这个匹配翻译成了具有特殊意义的字符了)。这个例子为什么能匹配在于, 元字符.指的是"任意字符".


API所支持的元字符有:([{\^-$|}])?*+.


注意:在学习过更多的如何构建正则表达式后,你会碰见这些情况:上面的这些特殊字符不应该被处理为元字符。然而也能够使用这一清单来检查一个特殊的字符是否会被认为是元字符。例如,字符!、@和#绝不会有特殊意义。


有两种方式可以强制将元字符处理成为普通字符:


1.在元字符前加上反斜线(\);


2.把它放在\Q(引用开始) 和 \E(引用结束)之间。在使用这种技术时,\Q 和\E能被放于表达式中的任何位置

?


5.字符类匹配


如果你曾看过Pattern类的说明,会看到一些构建正则表达式的概述。在这一节中你会发现一下的一些表达式:


?

Enter your regex: [bcr]atEnter input string to search: batI found the text "bat" starting at index 0 and ending at index 3.Enter your regex: [bcr]atEnter input string to search: catI found the text "cat" starting at index 0 and ending at index 3.Enter your regex: [bcr]atEnter input string to search: ratI found the text "rat" starting at index 0 and ending at index 3.?

?

?

在上面的例子中,在第一个字符匹配字符类中所定义字符类中的一个时,整个匹配就是成功的。



5.1.1 否定匹配


要匹配除那些列表之外所有的字符时,可以在字符类的开始处加上^元字符,这种就被称为否定(negation).


?

Enter your regex: [^bcr]atEnter input string to search: batNo match found.Enter your regex: [^bcr]atEnter input string to search: catNo match found.Enter your regex: [^bcr]atEnter input string to search: hatI found the text "hat" starting at index 0 and ending at index 3.
?

?

?

在输入字符串中的第一个字符不包括在字符类中所定义字符中的一个时,匹配是成功的。

?

5.1.2 范围


有时会想要定义一个包含范围的字符类,诸如,"a到h"的字母或者是"1"到"5"的数字。指定一个范围,只要在匹配的首字符和末字符间插入-元字符,比如:[1-5] 或者是 [a-h] 。也可以在类里每个的边上放置不同的范围来提高匹配的可能性,例如:[a-zA-Z]将会匹配a到z(小写字母)或者A到Z(大写字母)中任何一个字符。


下面是一些范围和否定的例子:


?

Enter your regex: [a-c]Enter input string to search: aI found the text "a" starting at index 0 and ending at index 1.Enter your regex: [a-c]Enter input string to search: bI found the text "b" starting at index 0 and ending at index 1.Enter your regex: [a-c]Enter input string to search: cI found the text "c" starting at index 0 and ending at index 1.Enter your regex: [a-c]Enter input string to search: dNo match found.Enter your regex: foo[1-5]Enter input string to search: foo1I found the text "foo1" starting at index 0 and ending at index 4.Enter your regex: foo[1-5]Enter input string to search: foo5I found the text "foo5" starting at index 0 and ending at index 4.Enter your regex: foo[1-5]Enter input string to search: foo6No match found.Enter your regex: foo[^1-5]Enter input string to search: foo6I found the text "foo6" starting at index 0 and ending at index 4.
?

?

?

5.1.3 并集


可以使用并集(union)来建一个由两个或两个以上字符类所组成的单字符类。构建一个并集,只要在一个字符类的边上嵌套另外一个,比如:[0-4[6-8]],这种奇特方式构建的并集字符类,可以匹配0,1,2,3,4,6,7,8这几个数字。


?

Enter your regex: [0-4[6-8]]Enter input string to search: 0I found the text "0" starting at index 0 and ending at index 1.Enter your regex: [0-4[6-8]]Enter input string to search: 5No match found.Enter your regex: [0-4[6-8]]Enter input string to search: 6I found the text "6" starting at index 0 and ending at index 1.Enter your regex: [0-4[6-8]]Enter input string to search: 8I found the text "8" starting at index 0 and ending at index 1.Enter your regex: [0-4[6-8]]Enter input string to search: 9No match found.
?

?

5.1.4 交集


建一个仅仅匹配自身嵌套类中公共部分字符的字符类时,可以像[0-9&&[345]]中那样使用&&.这种方式构建出来的交集(intersection)简单字符类,仅仅以匹配两个字符类的3,4,5共有部分。

?

?

Enter your regex: [0-9&&[345]]Enter input string to search: 3I found the text "3" starting at index 0 and ending at index 1.Enter your regex: [0-9&&[345]]Enter input string to search: 4I found the text "4" starting at index 0 and ending at index 1.Enter your regex: [0-9&&[345]]Enter input string to search: 5I found the text "5" starting at index 0 and ending at index 1.Enter your regex: [0-9&&[345]]Enter input string to search: 2No match found.Enter your regex: [0-9&&[345]]Enter input string to search: 6No match found.
?

?

?

下面演示两个范围交集的例子:


?

Enter your regex: [2-8&&[4-6]]Enter input string to search: 3I found the text "3" starting at index 0 and ending at index 1.Enter your regex: [2-8&&[4-6]]Enter input string to search: 4I found the text "4" starting at index 0 and ending at index 1.Enter your regex: [2-8&&[4-6]]Enter input string to search: 5I found the text "5" starting at index 0 and ending at index 1.Enter your regex: [2-8&&[4-6]]Enter input string to search: 6I found the text "6" starting at index 0 and ending at index 1.Enter your regex: [2-8&&[4-6]]Enter input string to search: 7No match found.
?

?

?

5.1.5 差集


最后,可以使用差集(subtraction)来否定一个或多个嵌套的字符类,比如:[0-9&&[^345]], 这个是构建一个匹配除3,4,5之外所有0-9间数字的简单字符类。


?

Enter your regex: [0-9&&[^345]]Enter input string to search: 2I found the text "2" starting at index 0 and ending at index 1.Enter your regex: [0-9&&[^345]]Enter input string to search: 3No match found.Enter your regex: [0-9&&[^345]]Enter input string to search: 4No match found.Enter your regex: [0-9&&[^345]]Enter input string to search: 5No match found.Enter your regex: [0-9&&[^345]]Enter input string to search: 6I found the text "6" starting at index 0 and ending at index 1.Enter your regex: [0-9&&[^345]]Enter input string to search: 9I found the text "9" starting at index 0 and ending at index 1.
?

?


到此为止,已经涵盖了如何建立字符类的部分。

?

6.预定义字符类

?

Pattern的API包有许多有用的预定义字符类(predefined character classes),提供了常用正则表达式的简写形式

private final String REGEX="\\d";?

这个例子中\d是正则表达式,另外的那个反斜线是用于代码编译所必须的。但是测试工具读取的表达式,是直接从控制台中输入的,因此不需要那个多出来的反斜线。

?

下面的例子说明了预定义字符类的用法:

?

?

Enter your regex: .Enter input string to search: @I found the text "@" starting at index 0 and ending at index 1.Enter your regex: .Enter input string to search: 1I found the text "1" starting at index 0 and ending at index 1.Enter your regex: .Enter input string to search: aI found the text "a" starting at index 0 and ending at index 1.Enter your regex: \dEnter input string to search: 1I found the text "1" starting at index 0 and ending at index 1.Enter your regex: \dEnter input string to search: aNo match found.Enter your regex: \DEnter input string to search: 1No match found.Enter your regex: \DEnter input string to search: aI found the text "a" starting at index 0 and ending at index 1.Enter your regex: \sEnter input string to search: No match found.Enter your regex: \sEnter input string to search: aNo match found.Enter your regex: \sEnter input string to search: " "I found the text " " starting at index 1 and ending at index 2.Enter your regex: \SEnter input string to search: aI found the text "a" starting at index 0 and ending at index 1.Enter your regex: \wEnter input string to search: aI found the text "a" starting at index 0 and ending at index 1.Enter your regex: \wEnter input string to search: !No match found.Enter your regex: \WEnter input string to search: !I found the text "!" starting at index 0 and ending at index 1.

?

在开始的三个例子中,正则表达式是最简单的,.("点"元字符)表示"任意字符",因此,在所有的三个例子(随意选取了"@"字符,数字和字母)中都是匹配成功的。在接下来的例子中,都使用了预定义的字符类表格中的单个正则表达式构造。你应该可以根据这张表指出前面每个匹配的逻辑:

?

\d 匹配数字字符

\s 匹配空白字符

\w 匹配单词字符

?

也可以使用意思正好相反的大写字母:

\D 匹配非数字字符

\S 匹配非空白字符

\W 匹配非单词字符

热点排行