java-正则表达式
来源:互联网 发布:神话电视剧知乎 编辑:程序博客网 时间:2024/05/19 17:06
部分一:熟悉类
1.String
String类中有两个正则表达式工具。matches()和split()方法。
public boolean matches(String regex) {
return Pattern.matches(regex,this);
}
public String[] split(String regex, int limit) {
return Pattern.compile(regex).split(this, limit);
}
public String[] split(String regex) {
return split(regex, 0);
}
java.util.regex包下有5个文件,分别为ASCII.JAVA,Matcher.java,MatchResult.java,Pattern.java,PatternSyntaxException.java.
MatchResult为interface,Matcher则实现这个接口。
在Matcher中内置一个Pattern对象。
2.Pattern
static Pattern compile(String regex)
将给定的正则表达式编译并赋予给Pattern类
static Pattern compile(String regex, int flags)
同上,但增加flag参数的指定,可选的flag参数包括:
CASE INSENSITIVE:不必考虑大小写
MULTILINE:多行模式下,'^','$'匹配行开头,结尾。
DOTALL:在此模式下,"."匹配所有字符。
CANON _EQ :完全匹配时才认为是匹配的。
split()
将输入字符串断开成字符串对象数组,断开边界参照正则表达式。
Matcher matcher(CharSequence input)
生成一个给定命名的Matcher对象
static boolean matches(String regex, CharSequence input)
编译给定的正则表达式并且对输入的字串以该正则表达式为模开展匹配,该方法适合于该正则表达式只会使用一次的情况,也就是只进行一次匹配工作,因为这种情况下并不需要生成一个Matcher实例。
3.MatchResult
public interface MatchResult {
public int start();
public int end();
public int end(int group);
public String group();//返回前一次匹配操作的整个匹配
public String group(int group);//
public int groupCount();//返回匹配器中组的数目
}
名词解释:组,是用括号划分的正则表达式,可以根据组的编号来引用某个组。组号0表示整个表达式,组号1表示被第一个括号括起的组。(来自[1] p302)
A(B(C))D 组0:ABCD
组1:BC
组2:C
4.Matcher
除了实现MatchResult的方法外,还有额外的替换方法。
replaceFirst()
replaceAll()
Matcher appendReplacement(StringBuffer sb, String replacement)
将当前匹配子串替换为指定字符串,并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里。
StringBuffer appendTail(StringBuffer sb)
将最后一次匹配工作后剩余的字符串添加到一个StringBuffer对象里
部分二:使用类
参考文献:
[1] 《thinking in java》第四版
- Java 正则表达式正则
- 【正则表达式】Java正则表达式
- 正则表达式 JAVA正则表达式
- 【Java】【正则表达式】正则表达式
- Java 正则表达式详解_正则表达式
- java正则表达式!正则表达式踩坑!!!
- 正则表达式工具类,正则表达式封装,Java正则表达式
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- java和正则表达式
- java正则表达式详解!
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- 在windows和linux安装与测试STLPort
- 家庭收支明细表[很强悍]
- Myeclipse下使用svn进行源码管理
- sql server round函数
- 什么是阻塞式和非阻塞io流?
- java-正则表达式
- gnuplot
- intent大全
- 在Qt creator/designer中安装Qwt6.0(Ubuntu)
- 关于教育改革的一些想法
- phing研究
- (未完)C++性能分析
- Linux下常用的操作命令
- win7中启动telnet