正则表达式(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
附:更多细节可查看JavaJDK的API。
0 0
- 正则表达式(regular expression)
- !!!正则表达式 (Regular Expression)
- 正则表达式(regular expression)
- 正则表达式(Regular Expression)
- 正则表达式(Regular Expression)
- 正则表达式(Regular Expression)
- 正则表达式(regular expression)
- 正则表达式(regular expression)
- 正则表达式(Regular Expression)
- 正则表达式(regular expression)
- 正则表达式 regular-expression
- 正则表达式(regular expression)
- 正则表达式(regular expression)
- 正则表达式(regular expression)
- 正则表达式(regular expression)
- 正则表达式(regular expression)
- 正则表达式(regular expression)
- 正则表达式(regular expression)
- 自定义 ActionBar
- ORACLE常用函数
- 如何挖掘和优化长尾关键词?
- pugixml 开篇
- 黑客日记:我是一个黑客
- 正则表达式(Regular Expression)
- Android事件传递机制
- 基于CMW500的数据应用测试
- 使用Xcode5.0上传应用到App Store
- 字符串以及正则表达式
- java多线程当中wait notify notifyAll的使用
- 利用spring的ApplicationContext在程序中唤醒quartz的job
- How to use a SQL Server database with your PHP web site
- android sleep and wakeup