package example.regularexpressions;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import junit.framework.TestCase;
public class Basics extends TestCase {
/**
* Pattern类:
* Pattern类的静态方法compile用来编译正则表达式,在此[,\\s]+表示若干个","或者若干个空格匹配
* split方法使用正则匹配将字符串切割成各子串并且返回
* @throws Exception
*/
public void _test1() throws Exception {
Pattern pattern = Pattern.compile("[,\\s]+");
String[] result = pattern.split("one two three,four , five,six");
for (String str : result) {
System.out.println(str);
}
}
/**
* Matcher类:
* 注意,Matcher的获得是通过Pattern.matcher(CharSequence charSequence);输入必须是实现了CharSequence接口的类
* 常用方法:
* matches()判断整个输入串是否匹配,整个匹配则返回true
* lookingAt()从头开始寻找,找到匹配则返回true
* @throws Exception
*/
public void _test2() throws Exception {
String str1 = "hello";
Pattern pattern1 = Pattern.compile("hello");
Matcher matcher1 = pattern1.matcher(str1);
System.out.println("matcher1.matches()=>" + matcher1.matches());
String str2 = "hello world";
Pattern pattern2 = Pattern.compile("hello");
Matcher matcher2 = pattern2.matcher(str2);
System.out.println("matcher2.matches()=>" + matcher2.matches());
System.out.println("matcher2.lookingAt()=>" + matcher2.lookingAt());
}
/**
* find()扫描输入串,寻找下一个匹配子串,存在则返回true
* @throws Exception
*/
public void _test3() throws Exception {
Pattern pattern = Pattern.compile("hello");
Matcher matcher = pattern.matcher("hello world, hello world, hello_world");
StringBuffer sb = new StringBuffer();
boolean find = matcher.find();
while(find) {
matcher.appendReplacement(sb, "haha"); //实现非终端添加和替换步骤
find = matcher.find();
System.out.println("sb=>" + sb);
}
matcher.appendTail(sb); //实现终端添加和替换步骤
System.out.println(sb.toString());
}
/**
* 匹配IP地址
* IP地址中的句点字符必须进行转义处理(前面加上“\”),因为IP地址中的句点具有它本来的含义,
* 而不是采用正则表达式语法中的特殊含义。句点在正则表达式中的特殊含义本文前面已经介绍。
* 日志记录的时间部分由一对方括号包围。你可以按照如下思路提取出方括号里面的所有内容:
* 首先搜索起始方括号字符(“[”),提取出所有不超过结束方括号字符(“]”)的内容,向前寻找直至找到结束方括号字符。
* @throws Exception
*/
public void _test4() throws Exception {
String logEntry = "192.168.0.1 - - [26/Feb/2009:14:56:43 -0500]\"GET /lsAlive/ht HTTP/1.0\"200 15\r\n"
+"192.168.0.2 - - [25/Feb/2009:14:56:43 -0500]\"GET /lsAlive/ht HTTP/1.0\"200 15";
String regexp = "([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})\\s-\\s-\\s\\[([^\\]]+)\\]";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(logEntry);
boolean find = matcher.find();
while(find) {
MatchResult result = matcher.toMatchResult();
System.out.println("IP=>" + result.group(1));
System.out.println("Timestamp=>" + result.group(2));
find = matcher.find();
}
}
/**
* HTML处理
* 分析HTML页面内FONT标记的所有属性
* @throws Exception
*/
public void _test5() throws Exception {
String html = "<font face=\"Arial Serif\" size=\"+2\" color=\"red\">";
String regexForTag = "<\\s*font\\s+([^>]*)\\s*>";
Pattern pattern = Pattern.compile(regexForTag, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(html);
boolean find = matcher.find();
String attribute = matcher.group(1);
System.out.println("属性字符串为:" + attribute);
String regexForAttribute = "([a-z]+)\\s*=\\s*\"([^\"]+)\"";
Pattern pattern2 = Pattern.compile(regexForAttribute, Pattern.CASE_INSENSITIVE);
Matcher matcher2 = pattern2.matcher(attribute);
boolean find2 = matcher2.find();
while(find2) {
MatchResult result = matcher2.toMatchResult();
System.out.println(result.group(1) + "=" + result.group(2));
find2 = matcher2.find();
}
}
/**
* HTML处理
* 修改一些页面中的链接
* @throws Exception
*/
public void test6() throws Exception {
String url = "<a href=\"http://192.168.0.1:8080/test/index.jsp#test...\">"
+ "< a href = \"http://192.168.0.1:8080/test/index.jsp#?hahahaha...\">";
String regex = "(<\\s*a\\s+href\\s*=\\s*\"http://192.168.0.1:8080/test/index.jsp[^\"]+\">)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(url);
boolean find = matcher.find();
System.out.println("find=>" + find);
while(find) {
MatchResult result = matcher.toMatchResult();
String temp = result.group(1);
System.out.println("替换前=>" + temp);
temp = temp.replace("192.168.0.1", "localhost");
System.out.println("替换后=>" + temp);
find = matcher.find();
}
}
/**
* 4种常用功能:
* 1、查询:
* 如果str中有regEx,那么rs为true,否则为flase。如果想在查找时忽略大小写,
* 则可以写成Pattern p=Pattern.compile(regEx,Pattern.CASE_INSENSITIVE);
* @throws Exception
*/
public void _testQuery() throws Exception {
String str = "abc efg ABC";
String regEx = "a|f";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(str);
boolean rs = matcher.find();
System.out.println("rs=>" + rs);
}
/**
* 2、提取:
* 执行结果为name.txt,提取的字符串储存在m.group(i)中,其中i最大值为m.groupCount();
* @throws Exception
*/
public void _testGet() throws Exception {
String regEx = ".+\\\\(.+)$";
String str = "c:\\dir1\\dir2\\name.txt";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(str);
boolean rs = matcher.find();
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}
/**
* 3、分割:
* @throws Exception
*/
public void _testSplit() throws Exception {
String regex = "::";
Pattern pattern = Pattern.compile(regex);
String[] result = pattern.split("aa::bb::cc");
for (String str : result)
System.out.println("result=>" + str);
System.out.println("---------");
String[] normal = "aa::bb::cc".split(regex);
for (String str : normal)
System.out.println("nornal=>" + str);
}
/**
* 4、替换(删除):
* 如果写成空串,既可达到删除的功能
* @throws Exception
*/
public void _testReplaceOrDelete() throws Exception {
String regex = "a+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("aaabbced a ccdeaa");
System.out.println("replaceFirst=>" + matcher.replaceFirst("A"));
String result = matcher.replaceAll("A");
System.out.println("replaceAll=>" + result);
String delete = matcher.replaceAll("");
System.out.println("替换为空即可达到删除的功能");
}
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
}
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
}
}
分享到:
相关推荐
本文写作时,一个包含了用正则表达式进行文本处理的Java规范需求(Specification Request)已经得到认可,你可以期待在JDK的下一版本中看到它。 然而,如果现在就需要使用正则表达式,又该怎么办呢?你可以从Apache...
Java正则表达式Java正则表达式Java正则表达式Java正则表达式
java,正则表达式,详解,java正则表达式,PDF
Java正则表达式 Java 正则表达式 图片版 携带方便,查阅方便!~
正则表达式之道.doc 正则表达式中的特殊字符.doc Java正则表达式详解.doc 正则表达式.ppt JAVA正则表达式--Pattern和Matcher.doc 例子
JAVA正则表达式应用:任意输入一串字符串 如何输入exit退出程序;从输入的字符串中判断是否包含手机号码 正则表达式可以使用" +86| 86 1 d{10}" 如果包含请将其在控制台打印出来 否则输出不包含字符串 ...
java正则表达式详解java正则表达式详解java正则表达式详解java正则表达式详解java正则表达式详解java正则表达式详解java正则表达式详解
java正则表达式验证IP地址
java使用正则表达式进行校验验证,主要使用了Pattern和Matcher类,直接main方法运行就可以,亲测有效
java正则表达式.pdfjava正则表达式.pdfjava正则表达式.pdfjava正则表达式.pdfjava正则表达式.pdf
Java正则表达式介绍和练习Java正则表达式介绍和练习Java正则表达式介绍和练习
Java正则表达式应用总结
JAVA实现的正则表达式转换成DFA,并将DFA用Graph画出,画图须安装Graph。
java正则表达式 过滤特殊字符的正则表达式
Jva正则表达式Jva正则表达式Jva正则表达式Jva正则表达式Jva正则表达式
正则表达式的使用 java正则表达式的使用示例 java正则表达式
java正则表达式使用例子,代码是Android的
在学习完该教程后,应该对正则表达式有了初步的了解,并能熟练地运用 java.util.regex 包中的关于正则表达式的类库,为今后学习更高级的正则表达式技术奠定良好的基础。 教程中所有的源代码都在 src 目录下,可以...
Java正则表达式详解.pdf Java正则表达式详解.pdf
正则表达式匹配工具很好很强大 放心使用 内附各种经典正则匹配公式