正则表达式(一)

  正则表达式是一种强大而灵活的文本处理工具。使用正则表达式,我们能够以编程的方式,构造复杂的文本模式,并对输入的字符串进行搜索。找到匹配这些模式的部分就可以对它处理。正则表达式提供了一种完全通用的方式,能够解决各种字符串处理相关的问题:匹配、选择、编辑以及验证。

1、基本语法

字符:
B                 指定字符B
\xhh             十六进制为oxhh的字符
\uhhhh            十六进制表示为oxhhhh的Unicode字符
\t                制表符Tab
\n                换行符
\r                回车
\f                换页
\e                转义


字符类:
.                 任意字符
[abc]             包含a、b、c的任意字符(同a|b|c)
[^abc]            除了a、b、c之外的任意字符(否定)
[a-zA-z]          a-z或者A-Z的任意字符(范围)
[abc[hij]]        同a|b|c|h|i|j。合并
[a-z&&[hij]]      任意h、i或j(交)
\s                空白符(空格、tab、换行、换页和回车)
\S                非空白符([^\s])
\d                数字[0-9]
\D                非数字[^0-9]
\w                词字符[a-zA-Z0-9]
\W                [^\w]


逻辑操作符:
XY                Y跟在X后面
X|Y               X或者Y
(X)               捕获组。可以在表达式中用\i引用第i个捕获组


边界匹配符
^                一行的起始
$                一行的结束
\b               词的边界
\B               非词的边界
\G               前一个匹配的结束

 

2、应用正则表达式的最简单的途径,就是利用String类内建的功能。先来看一个例子:

1 public class IntegerMatch {
2     public static void main(String[] args) {
3         System.out.println("-1234".matches("-?\\d+"));
4         System.out.println("5678".matches("-?\\d+"));
5         System.out.println("+911".matches("-?\\d+"));
6         System.out.println("+911".matches("(-|\\+)?\\d+"));
7     }
8 }

输出:

true
true
false
true

本例中前两个字符串对应的正则表达式,匹配成功。第三个字符串开头有一个“+”,他也是一个整数,但与正则表达式却不匹配,所以应该用第四个正则描述:

(-|\\+)? :表示可能有一个“+”或“-”或无符号开头。其中"()"表示分组,"|"表示或。因为"+"在正则表达式中有着特殊的含义,所以必须使用\\进行转义成一个普通字符。

 

3、split()方法,String类自带一个非常有用的正则表达式工具--split()方法,其功能是将字符串从正则表达式匹配的地方切开。

 1 public class Splitting {
 2     public static String str =
 3             "Then, when you have found the shrubbery, you must " +
 4             "cut down the mightiest tree in the forest... " +
 5             "with... a herring!";
 6     public static void split(String regex){
 7         System.out.println(Arrays.toString(str.split(regex)));
 8     }
 9     public static void main(String[] args) {
10         split(" ");
11         split("\\W+");
12         split("n\\W+");
13     }
14 }

输出:

[Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest..., with..., a, herring!]
[Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring]
[The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest... with... a herring!]

  首先看第一个语句,按空格划分字符串。

  第二个和第三个都有用到了\W (非单词字符),通过第二个可以看到,它将标点字符删除了。第三个表示"字母n后面跟着一个或者多个非单词字符",可以看到,在原始字符中,与正则表达式匹配的部分,在最终的结果中都不存在了。

  String.split()还有一个重载的版本,增加了限制字符串分割次数的参数。

 

4、替换方法,String类自带的最后一个正则表达式工具是"替换"。可以选择只替换正则表达式第一匹配的字串也可以替换所有匹配的地方。

1 public class Replacing {
2     static String s = Splitting.str;//接上个类Splitting
3     public static void main(String[] args) {
4         System.out.println(s.replaceFirst("f\\w+", "located"));//替换第一个匹配成功的字串
5         System.out.println(s.replaceAll("shrubbery|tree|herring", "banana"));//替换所有匹配成功的字串
6     }
7 }

输出:

Then, when you have located the shrubbery, you must cut down the mightiest tree in the forest... with... a herring!
Then, when you have found the banana, you must cut down the mightiest banana in the forest... with... a banana!

  第一个表达式匹配以字母f开头,后面跟一个或多个字母。只替换第一匹配的部分,所以"found"被替换成"located"。

  第二个表达式要匹配的是三个单词中的任意一个,并替换掉所有匹配的部分。

 

 

posted @ 2015-07-28 15:48  codingblock  阅读(397)  评论(0编辑  收藏  举报