正则表达式
来源:互联网 发布:js和jsp交互 编辑:程序博客网 时间:2024/05/17 07:31
正则表达式
目录
- 正则表达式
- 目录
- 正则表达式的规则
- 字符
- 字符类
- 预定义字符类
- POSIX 字符类仅 US-ASCII
- javalangCharacter 类简单的 java 字符类型
- Unicode 块和类别的类
- 边界匹配器
- Greedy 数量词贪婪模式
- Reluctant 数量词勉强模式
- Possessive 数量词 占有模式
- Logical 运算符
- 正则表达式的用法
- 正则表达式的用法匹配字符串
- 正则表达式的用法替换分割
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。(来源于百度百科)
正则表达式的规则
字符
字符类
预定义字符类
POSIX 字符类(仅 US-ASCII)
java.lang.Character 类(简单的 java 字符类型)
Unicode 块和类别的类
边界匹配器
所谓的单词边界就是不能用\w全部匹配
举例:xiaoming\b
要匹配的字符xiaoming xiaoming222 xiaoming2()2
能匹配到的是第一个和第三个,因为第二个后面的全部都能用\w匹配。
Greedy 数量词(贪婪模式)
Greedy模式:即贪婪模式,此模式匹配字符时一直会匹配下去,直到无法匹配。
Reluctant 数量词(勉强模式)
每一个都比Greedy多一个?
勉强模式:匹配时是最小的字符。它与贪婪模式的对比如下。
public class GuiZeTest {public static void main(String[] args) { String str = "aaa, bbb"; System.out.println(str.replaceFirst("\\w*", "+")); // +, bbb System.out.println(str.replaceFirst("\\w*?", "+")); // +aaa, bbb System.out.println(str.replaceFirst("\\w*+", "+"));}
}
Possessive 数量词 (占有模式)
每一个都比Greedy多一个+
占有模式:只有Java才有的模式,一般较少使用。
Logical 运算符
正则表达式的用法
指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。
典型的调用顺序是:
Pattern p = Pattern.compile("a*b");Matcher m = p.matcher("aaaaab");boolean b = m.matches();
Pattern compile(String regex)
将给定的正则表达式编译到模式中。
public Matcher matcher(CharSequence input)
创建一个匹配器,匹配给定的输入与此模式。
参数
input - 要匹配的字符序列
结果
这种模式的新匹配器
public static boolean matches(String regex CharSequence input)
编译给定的正则表达式,并尝试匹配给定的输入。
调用这种方便的方式的形式 Pattern.matches(regex, >input);表现方式与表达式完全相同
Pattern.compile(regex).matcher(input).matches()
如果一个模式多次被使用,编译一次并重用它将比每次调用此方法更有效。
参数
regex - 要编译的表达式
input - 要匹配的字符序列
结果
正则表达式是否匹配输入
由上文可知上面的三行代码可以写成: boolean b = Pattern.matches("aaaaaab", "a*b")
正则表达式的用法:匹配字符串
import java.util.regex.*;public class FindGroup {public static void main(String[] args) { String str = "这儿有一些电话号码,我只需要13和15开头的电话号码。" + "13472011907,15189787288, 1789829828,1293141942"; Matcher m = Pattern.compile("((13\\d)|(15\\d))\\d{8}").matcher(str); while(m.find()) { System.out.println(m.group()); } }}
find() : 返回目标中是否与Pattern中匹配的字符串
group() : 返回上一次与Pattern匹配的字符串
start() : 返回上一次与Pattern匹配字符串的开始位置(在原字符串中)
end() : 返回上一次与Pattern匹配字符串的结束位置加1
lookAt() : 返回目标字符串与Pattern是否匹配,此方法与matches()比较相似,但不同的是它不需要匹配整个字符串。
reset : 重新设置需要匹配的字符串
matches() : 返回目标是否与Pattern是否匹配
下面是一些示例:
import java.util.regex.*;public class StartAndEnd { public static void main(String[] args) { String str = "Java is very easy"; System.out.println("目标字符串是:" + str); Pattern p = Pattern.compile("\\w+"); Matcher m = p.matcher(str); while(m.find()) { System.out.println(m.group() + "起始位置:" + m.start() + "结束位置:" + m.end()); } }}
import java.util.regex.*;public class MatchesTest { public static void main(String[] args) { String mails[] = { "1293141942@qq.com", "mengchen@xiyou3g.com", "ppap@abbc.org", "ascsc@cacs.asa" }; String pattern = "\\w{3,20}@\\w+\\.(com|net|cn|org|gov)"; Pattern mp = Pattern.compile(pattern); Matcher mm = null; for(String mail : mails) { if(mm == null) { mm = mp.matcher(mail); } else { mm.reset(mail); } System.out.println(mail + (mm.matches() ? "是" : "不是") + "一个邮箱地址。"); } }}
事实上String中也提供了matches()方法 mials[0].matches("\\w{3,20}@\\w+\\.(com|net|cn|org|gov)")
正则表达式的用法:替换、分割
import java.util.regex.*;public class ReplaceTest { public static void main(String[] args) { String[] str = { "Java is easy to study", "I love Java", "I don't konw to write something about Java" }; Pattern p = Pattern.compile("Ja\\w+"); Matcher m = null; for (String x : str) { if (m == null) { m = p.matcher(x); } else { m.reset(x); } System.out.println(m.replaceAll("C语言")); } }}
Matcher类提供了替换字符串的方法
replaceAll():替换全部能匹配到的
replaceFirst() : 替换第一个匹配到的。
事实上,Java的String类也提供了这几个方法。
import java.util.Arrays;public class StringReplaceTest { public static void main(String[] args) { String[] str = { "Java is easy to study", "I love Java", "I don't konw to write something about Java" }; for(String x : str) { System.out.println(x.replaceAll("java\\w+", "C语言")); System.out.println(Arrays.toString(x.split(" "))); } }}