java regex 正则部分高级特性使用
2012-03-17 12:26
696 查看
package unit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.junit.Test;
public class RegexUnit {
@Test
/**
* <p>向前\向后查找</p>
*/
public void unit9()throws Exception{
String testStr = "http://www.google.com";
/* 一般查找
* .+(:) 查询出结果包含 :
*/
Pattern pattern = Pattern.compile(".+(:)");
Matcher matcher = pattern.matcher(testStr);
Assert.assertTrue("错误: 查找出结果通过 .+(:) 此regex没有包含 : ",
matcher.find() && matcher.group().equals("http:") );
/* 向前查找
* .+(?=:) 查询结果不包含 :
*/
Pattern pattern2 = Pattern.compile(".+(?=:)");
Matcher matcher2 = pattern2.matcher(testStr);
Assert.assertTrue("错误: 查找出结果通过 .+(?=:) 此regex有包含 : ",
matcher2.find()&& matcher2.group().equals("http"));
/* 向后查找
* (?<=:).+
*/
Pattern pattern3 = Pattern.compile("(?<=://).+");
Matcher matcher3 = pattern3.matcher(testStr);
Assert.assertTrue("错误:查找出结果包含 http:// 不向后查询",
matcher3.find()&& matcher3.group().equals("www.google.com") );
}
@Test
/** 回朔应用
* 查询回朔、回朔替换、回朔大小写替换
*/
public void unit8()throws Exception{
String testStr = "this is a block of of test,"+
" several words here are are " +
" repeated , and and they should not be. ";
Pattern pattern = Pattern.compile("[ ]+(\\w+)[ ]+\\1");
Matcher matcher = pattern.matcher(testStr);
//查询结果 are are
Assert.assertTrue("错误:regex 不匹配 ",
matcher.find()&&matcher.group().split(" ").length>=2 );
while( matcher.find() ){
Assert.assertTrue("错误:regex 不匹配 ",
matcher.group().split(" ").length>=2 );
}
//替换
String testStr2s = "313-555-1234";
Pattern pattern2 = Pattern.compile("(\\d{3})(-)(\\d{3})(-)(\\d{4})");
Matcher mtmp = pattern2.matcher(testStr2s);
Assert.assertTrue("错误:没有查替换",
mtmp.find() &&
mtmp.replaceAll("($1) $3-$5").equals("(313) 555-1234") );
/*大小写替换(java 不能成功)
* \E 结束 \L 或 \U转换
* \l \L 把下一个字符(串)换为小写
* \ u \U 把下一个字符(串)转换为大写
* 此中 (.*?)懒散加载
*/
String testStr3 = "tt:google:xx";
Pattern pattern3 = Pattern.compile("(?<=:)(.*?)(?=:)");
Matcher matcher2 = pattern3.matcher(testStr3);
if( matcher2.find())
System.out.println( matcher2.group() ) ;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.junit.Test;
public class RegexUnit {
@Test
/**
* <p>向前\向后查找</p>
*/
public void unit9()throws Exception{
String testStr = "http://www.google.com";
/* 一般查找
* .+(:) 查询出结果包含 :
*/
Pattern pattern = Pattern.compile(".+(:)");
Matcher matcher = pattern.matcher(testStr);
Assert.assertTrue("错误: 查找出结果通过 .+(:) 此regex没有包含 : ",
matcher.find() && matcher.group().equals("http:") );
/* 向前查找
* .+(?=:) 查询结果不包含 :
*/
Pattern pattern2 = Pattern.compile(".+(?=:)");
Matcher matcher2 = pattern2.matcher(testStr);
Assert.assertTrue("错误: 查找出结果通过 .+(?=:) 此regex有包含 : ",
matcher2.find()&& matcher2.group().equals("http"));
/* 向后查找
* (?<=:).+
*/
Pattern pattern3 = Pattern.compile("(?<=://).+");
Matcher matcher3 = pattern3.matcher(testStr);
Assert.assertTrue("错误:查找出结果包含 http:// 不向后查询",
matcher3.find()&& matcher3.group().equals("www.google.com") );
}
@Test
/** 回朔应用
* 查询回朔、回朔替换、回朔大小写替换
*/
public void unit8()throws Exception{
String testStr = "this is a block of of test,"+
" several words here are are " +
" repeated , and and they should not be. ";
Pattern pattern = Pattern.compile("[ ]+(\\w+)[ ]+\\1");
Matcher matcher = pattern.matcher(testStr);
//查询结果 are are
Assert.assertTrue("错误:regex 不匹配 ",
matcher.find()&&matcher.group().split(" ").length>=2 );
while( matcher.find() ){
Assert.assertTrue("错误:regex 不匹配 ",
matcher.group().split(" ").length>=2 );
}
//替换
String testStr2s = "313-555-1234";
Pattern pattern2 = Pattern.compile("(\\d{3})(-)(\\d{3})(-)(\\d{4})");
Matcher mtmp = pattern2.matcher(testStr2s);
Assert.assertTrue("错误:没有查替换",
mtmp.find() &&
mtmp.replaceAll("($1) $3-$5").equals("(313) 555-1234") );
/*大小写替换(java 不能成功)
* \E 结束 \L 或 \U转换
* \l \L 把下一个字符(串)换为小写
* \ u \U 把下一个字符(串)转换为大写
* 此中 (.*?)懒散加载
*/
String testStr3 = "tt:google:xx";
Pattern pattern3 = Pattern.compile("(?<=:)(.*?)(?=:)");
Matcher matcher2 = pattern3.matcher(testStr3);
if( matcher2.find())
System.out.println( matcher2.group() ) ;
}
}
相关文章推荐
- java.util.regex 包中自带的两个 正则表达式类的使用总结
- 使用 Dojo 的 Ajax 应用开发进阶教程,第 1 部分: JavaScript 技巧与高级特性
- java.util.regex包--正则表达式的使用
- 总结redis第二部分(redis常用命令、高级命令特性以及与java代码的结合)
- Java的一些高级特性(三)——类部分
- JAVA JDK5的特性、eclipse简介和使用、正则表达式
- [零基础学JAVA]Java SE应用部分-22.Eclipse及正则表达式使用 推荐
- Java的一些高级特性(二)——初始化和赋值部分
- java中split(regex)使用中要注意的问题:正则表达式
- Java高级特性部分(继承与多线程)
- java中split(regex)使用中要注意的问题:正则表达式
- 【java高级特性之反射】Field类和Constructor类的使用
- 字符串处理是许多程序中非常重要的一部分,它们可以用于文本显示,数据表示,查找键和很多目的.在Unix下,用户可以使用正则表达式的强健功能实现这些 目的,从Java1.4起,Java核心API就引入了java.util.regex程序包,它是一种有价值的基础
- java中split(regex)使用中要注意的问题:正则表达式
- java中提供了对正则表达式的支持。 有的时候,恰当地使用正则,可以让我们的工作事半功倍! 如下代码用来检验一个四则运算式中数据项的数目,请填写划线部分缺少的代码。 注意:只填写缺少代码,不要
- JAVA正则表达式的使用(待整理)
- java高级工程师-----JSON和XML的使用
- 如何使用Java自带的正则表达式
- Java_正则表达式_推荐在复杂字符串操作中使用正则表达式
- java高级特性_2