正则表达式(Regular Expression)

来源:互联网 发布:7.0魔兽数据库 野兽 编辑:程序博客网 时间:2024/05/05 06:42

一:Java中的正则(regex)

典型的调用顺序是(java.util.regex.Pattern)

 Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches();

[^abc] 任何字符,除了 a、b 或 c(否定) 
^ 行的开头 
$ 行的结尾 
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集) 
[a-z&&[def]] d、e 或 f(交集) 
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去) 
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去) 
\\\\ 匹配\ (必须是4个"\")

实例:

不区分大小写

StringBuffer s = new StringBuffer("");p = Pattern.compile("name", Pattern.CASE_INSENSITIVE);m = p.matcher("Namepppp nAMe NAME name All");print(m.replaceAll("NAME"));

奇偶替换

StringBuffer s = new StringBuffer("");p = Pattern.compile("name", Pattern.CASE_INSENSITIVE);m = p.matcher("Namepppp nAMe NAME name All");int i = 0;while (m.find()) {i++;if (i % 2 == 0) {m.appendReplacement(s, "NAME");} else {m.appendReplacement(s, "name");}}m.appendTail(s);print(s);

分组

p = Pattern.compile("(\\d{2,4}([a-z]{2,}))");m = p.matcher("12a345bc6789def");while(m.find()) {print(m.group(1));}m.reset();while(m.find()) {print(m.group(2));}


检测邮箱并统计

while((line = br.readLine()) != null) {Pattern p = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");Matcher m = p.matcher(line);while (m.find()) {print(m.group());}}

统计代码

String line = "";Boolean comment = false;while((line = br.readLine()) != null) {if (line.matches("^[\\s&&[^\\n]]*$")) {whiteLine++;} else if (line.startsWith("\t/*") && !line.endsWith("*/")) {commentLine++;comment = true;} else if (true == comment) {commentLine++;if (line.endsWith("*/")) {comment = false;}} else if (line.startsWith("//")) {commentLine++;} else {codeLine++;}}

贪婪模式

p = Pattern.compile(".{3,5}[0-9]");m = p.matcher("0123456789ba");if (m.find()) {p(m.start()+"-"+m.end());} else {p("Not Match!");}m.reset();p(m.matches());
等价于

p = Pattern.compile(".{3,5}[0-9]{1}");m = p.matcher("0123456789ba");if (m.find()) {p(m.start()+"-"+m.end());} else {p("Not Match!");}m.reset();p(m.matches());

特殊构造(非捕获)

p = Pattern.compile(".{1}(?=a)");m = p.matcher("1a2a3b");while (m.find()) {p(m.group());}p = Pattern.compile("(?=a).{1}");m = p.matcher("1a2a3b");while (m.find()) {p(m.group());}p = Pattern.compile(".{1}(?!a)");m = p.matcher("1a2a3b");while (m.find()) {p(m.group());}p = Pattern.compile("(?!a).{1}");m = p.matcher("1a2a3b");while (m.find()) {p(m.group());
更多引用请查看API


Back 引用(组,以左括号为组的序号)

p = Pattern.compile("(\\d\\d\\d)\\1");m = p.matcher("123123");p(m.matches());//truep = Pattern.compile("((\\d)\\d\\d)\\2");m = p.matcher("1231");p(m.matches());//truep = Pattern.compile("(\\d(\\d\\d))\\2");m = p.matcher("12323");p(m.matches());//true


除了使用Pattern还可以使用String.matches(String regex);方式。该方式还等价于:boolean b = Pattern.matches("a*b", "aaaaab");都无法使用已编译模式。从效率上考虑使用Pattern最好。

附:更多细节可查看JavaJDK的API。

0 0
原创粉丝点击