一.基础
1.一般来说正则表达式就是以某种方式来描述字符串,因此你可以说如果一个字符串中含有这些东西,那么它就是我正在找的东西."例如,要找一个数字,它可能有一个负号在最前面,那么你就写一个负号加上一个问号,就像这样: -?
2. 在Java中使用正则表达式, \\ 的意思是要插入一个正则表达式的反斜线, \\\\ 是插入一个普通的反斜线.
3.要表示"一个或多个之前的表达式",因该使用+,所以,如果要表示"可能有一个负号,后面紧跟一位或多位数字",可以这样: -?\\d+
4.应用正则表达式的最简单途径,就是利用String类内建的功能,例如你可以检查一公分String是否匹配如上所述表达式
packagestrings;//: strings/IntegerMatch.java
public classIntegerMatch {public static voidmain(String[] args) {
System.out.println("-1234".matches("-?\\d+"));//有负号
System.out.println("5678".matches("-?\\d+")); //没有负号
System.out.println("+911".matches("-?\\d+")); //有加号
System.out.println("+911".matches("(-|\\+)?\\d+"));//在正则表达式中,括号有着将表达式分组的效果,而竖线则表示或操作
}
}/*Output:
true
true
false
true*///:~
5. String还自带了一个非常有用的正则表达式工具-split()方法,其功能是"将字符从正则表达式配备的地方切开.String还有一个重载的版本,它允许你限制字符串分割的次数
packagestrings;//: strings/Splitting.java
import java.util.*;public classSplitting {public static String knights =
"Then, when you have found the shrubbery, you must " +
"cut down the mightiest tree in the forest... " +
"with... a herring!";public static voidsplit(String regex) {
System.out.println(
Arrays.toString(knights.split(regex)));
}public static voidmain(String[] args) {
split(" "); //Doesn't have to contain regex chars
split("\\W+"); //Non-word characters
split("n\\W+"); //'n' followed by non-word characters
}
}/*Output:
[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!]*///:~
6.Stirng自带的最后一个正则表达式工具是"替换",你可以只替换正则表达式第一个配备的字串,或是替换所有匹配的地方
packagestrings;//: strings/Replacing.java
import static net.mindview.util.Print.*;public classReplacing {static String s =Splitting.knights;public static voidmain(String[] args) {
print("\\\\");
print(s.replaceAll("shrubbery|tree|herring","banana"));
}
}/*Output:
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!*///:~
7.创建正则表达式
字符说明
B
指定字符B
\xhh
十六进制值为oxhh的字符
\uhhhh
十六进制表示为oxhhhh的Unicode字符
\t
制表符Tab
\n
换行符
\r
回车
\f
换页
\e
转移(Escape)
字符类说明
字符类说明
.
任意字符
[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或b或c或h或i或j作用相同)(合并)
[a-z&&[hij]]
任意的h,i或j(交)
\s
空白符(空格,tab,换行,换页和回车)
§
非空白符([^\s])
\d
数组[0-9]
\D
非数字[0-9]
\w
词字符[a-zA-Z0-9]
\W
非此字符[^\w]
边界匹配符
边界匹配符说明
^
一行的起始
\B
非词的边界
$
一行的结束
\G
前一个匹配的结束
\b
词的边界
二.量词
1.量词描述了一个模式吸收输入文本的方式
贪婪型: 量词总是贪婪的,除非有其他的选项被设置.贪婪型表达式会为所有可能的模式发现尽可能多的匹配.导致此问题的一个典型理由就是嘉定我们的模式技能匹配第一个可能的字符组,如果它是贪婪的,那么它就会继续往下匹配.
勉强型: 用问号来制定,这个量词匹配满足模式所需的最少字符数,也称作懒惰的,最少匹配的,非贪婪的,不贪婪的.
占有型: 目前,这种类型的量词只有在Java中才可用.当正则表达式被应用于字符串时,它会产生相当多的状态,以便在匹配失败的时候可以回溯.它们常常可以用于防止正则表达式时空,因此可以使用正则表达式执行起来更有效.
贪婪型勉强型占有型如何匹配
X?
X??
X?+
一个或零个X
X*
X*?
X*+
零个或多个X
X+
X+?
X++
一个或多个X
X{n}
X{n}?
X{n}+
恰好n次X
X{n,}
X{n,}?
X{n,}+
至少n次X
X{n,m}
X{n,m}?
X{n,m}+
X至少n次,且不超过m次
三..Pattern和Matcher
Pattern.compile()会更加String类型的正则表达式生成一个Pattern对象.
Pattern对象的matcher()方法会生成一个Matcher对象.
Matcher的方法,能够判断各种不同配型的匹配是否成功
boolean matches() //用来判断整个输入字符串是否匹配正则表达式模式
boolean lookingAt()//用来判断改字符串(不必式真个字符串)的始部分是否能够匹配模式
boolean find() //用来在CharSequence中查找多个匹配的字符
boolean find(int start) //从start开始查找
replaceAll() //将所有匹配的部分替换成传入的参数
int groupCount()返回该匹配器的模式中的分组数目,第0组不包括在内
String group() //返回前一次匹配操作
String group(int i)返回在前一次匹配操作期间指定的组合
int start()返回在前一次匹配中寻找到的组的起始索引
int end()//返回在前一次匹配中寻扎到的组的最后一个索引加1的值
public boolean lookingAt() //只有在正则表达式与输入的最开始处就开始匹配才会成功, 只要输入的第一部分匹配就会成功.
public String[] split(CharSequence input): 将输入字符串断开成字符串对象数组,断开边界由正则表达式决定.
public String[] split(CharSequence input, int limit)// 将输入字符串断开成字符串对象数组,断开边界由下列正则表达式决定,limit限定将输入分割成字符串的数量.
public replaceFirst(String replacement)//以参数字符串replacement替换掉第一个匹配成功的部分.
public replaceAll(String replacement)//以参数字符串replacement替换掉所有匹配成功的部分.
组
组是用括号划分的正则表达式,可以根据组的编号来引用某个组.组号为0表示整个表达式,组号为1表示被一对括号括起的组,一次类推.
Matcher对象提供了一系列方法
public int groupCount(): 该匹配器的模式中分组的数目,第0组不包括在内.
public String group(): 返回前一次匹配操作的第0组.
public String group(int i): 返回前一次匹配操作期间指定的组号,如果匹配成功,但是指定的组没有匹配输入字符串的任何部分,则将会返回null.
3.start()和end()
public static String replace(String html, String nReplace, String nameValue) {
String str = html;
Pattern pattern = Pattern.compile(">" + nReplace);
Matcher matcher = pattern.matcher(html);
if(nameValue !=null )
str = matcher.replaceAll(">" +nameValue);
else
str = matcher.replaceAll(">");
return str;
}