您的位置:首页 > 编程语言 > Java开发

JDK6笔记(4)----正则表达式2

2007-03-28 19:55 344 查看
JDK6笔记(4)----正则表达式2

一、组group
1、组是由圆括号分开的正则表达式,随后可以根据它们的组号进行调用。
第0组匹配整个表达式,第1组匹配第1个圆括号扩起来的组,......依次类推。
如:A(B(C))D
有3个组:
第0组:ABCD
第1组:BC
第2组:C

例子:
package myfile;
import java.util.regex.*;
public class GroupR2 {
public static void main(String[] args) {
String[] input=new String[]{
"Java has regular expressions in 1.4",
"regular expressions now expressing in Java",
"Java represses oracular expressions"
};
Pattern
p1=Pattern.compile("re//w*"),
p2=Pattern.compile("Java.*");
for(int i=0;i<input.length;i++){
System.out.println("input "+i+":"+input[i]);
Matcher
m1=p1.matcher(input[i]),
m2=p2.matcher(input[i]);
while(m1.find())
System.out.println("m1.find() '"+m1.group()+"' start= "+m1.start()+" end= "+m1.end());
while(m2.find())
System.out.println("m2.find() '"+m2.group()+"' start= "+m2.start()+" end= "+m2.end());
if(m1.lookingAt())
System.out.println("m1.lookingAt() start = "+m1.start()+" end= "+m1.end());
if(m2.lookingAt())
System.out.println("m2.lookingAt() start = "+m2.start()+" end= "+m2.end());
if(m1.matches())
System.out.println("m1.matches() start= "+m1.start()+" end= "+m1.end());
if(m2.matches())
System.out.println("m2.matches() start= "+m2.start()+" end= "+m2.end());

}
}
/**
* 输u20986 结u26524 :
input 0:Java has regular expressions in 1.4
m1.find() 'regular' start= 9 end= 16
m1.find() 'ressions' start= 20 end= 28
m2.find() 'Java has regular expressions in 1.4' start= 0 end= 35
m2.lookingAt() start = 0 end= 35
m2.matches() start= 0 end= 35
input 1:regular expressions now expressing in Java
m1.find() 'regular' start= 0 end= 7
m1.find() 'ressions' start= 11 end= 19
m1.find() 'ressing' start= 27 end= 34
m2.find() 'Java' start= 38 end= 42
m1.lookingAt() start = 0 end= 7
input 2:Java represses oracular expressions
m1.find() 'represses' start= 5 end= 14
m1.find() 'ressions' start= 27 end= 35
m2.find() 'Java represses oracular expressions' start= 0 end= 35
m2.lookingAt() start = 0 end= 35
m2.matches() start= 0 end= 35
*/
}

2、Matcher对象的方法:
int groupCount() 分组的数目(不含0组)
String group() 返回前一次的匹配操作
String group(int i) 返回前一次匹配操作期间指定的组
int start(int group) 返回前一次匹配操作寻找到的组的起始下标
int end(int group) 返回前一次匹配操作寻找到的组的最后一个字符下标加一的值
二、模式标记
Pattern Pattern.compile(String regex, int flag)
flag有多个值:
(1)Pattern.CANON_EQ 两个字符当且仅当它们的完全规范分解相匹配时,就认为匹配。缺省时,不考虑。
(2)Pattern.CASE_INSENSITIVE 缺省时,仅在ASCII字符集中进行。
(3)Pattern.COMMENTS 忽略空格符,且以#号开始到行末的注释也忽略
(4)Pattern.DOTALL 表达式'.'匹配所有字符,包括行终结符。缺省时,'.'不匹配行终结符。
(5)Pattern.MULTILINE 在多行模式下,表达式‘^'和'$'分别匹配一行的开始和结束。缺省时,它们仅匹配输入的完整字符串的开始和结束。
见例子:

package myfile;
import java.util.regex.*;
public class ReFlags {
public static void main(String[] args) {
String str="java has regex/nJava has regex/n" +
"JaVa has pretty good regular expressions/n"+
"Regular expressions are in JAva";
Pattern p=Pattern.compile("^java", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher m=p.matcher(str);
while(m.find()) //find()尝u-29739 查u25214 与u-29723 模u24335 匹u-28339 的u-28781 入u24207 列u30340 下u19968 个u23376 序u21015 。
System.out.println(m.group()); //group()返u22238 由u20197 前u21305 配u25805 作u25152 匹u-28339 的u-28781 入u23376 序u21015 。
}
}

三、split()
它将输入字符串断开成字符串对象数组,断开边界由正则表达式确定。
String split(CharSequence charseq);
String split(CharSequence charseq, int limit);
第2种limit限制了分裂的数目。

例子:

package myfile;
import java.util.regex.*;
import java.util.*;
public class SplitDemo {
static String input="This!!unusual use!!of exclamation!!points";
public static void main(String[] args) {
System.out.println(Arrays.asList(Pattern.compile("!!").split(input)));
//Arrays.asList() 返回一个受指定数组支持的固定大小的列表。
System.out.println(Arrays.asList(Pattern.compile("!!").split(input,3)));
System.out.println(Arrays.asList("Aha! String has a split() built in!".split(" ")));
}
}

四、替换操作
1)replaceFirst(String replacement)
用replacement替换输入字符串中最先匹配的那部分。
2)replaceAll(String replacement)
用replacement替换输入字符串中所有的匹配部分。
3)appendReplacement(StringBuffer sbuf, String replacement)
逐步地在sbuf中执行替换
4)appendTail(StringBuffer sbuf,String replacement)
在一个或多个appendReplacement()调用之后被调用,以便复制输入字符串的剩余部分。

例子:

package myfile;
import java.util.regex.*;
import java.io.*;
/*!Here's a block of text to use as input to
* the regular expression matcher. Note that we'll
* first extract the block of text by looking for
* the special delimiters, then process the
* extracted block.!
*/
public class TheReplacements {
public static void main(String[] args) throws Exception{
String s="/*!Here's a block of text to use as input to/n"+
" the regular expression matcher. Note that we'll/n"+
"first extract the block of text by looking for/n"+
"the special delimiters, then process the/n"+
"extracted block.!*/";
Pattern p=Pattern.compile("///*!(.*)!//*/", Pattern.DOTALL); //用以匹配在‘/*!’和‘!*/’之间的所有文本
Matcher mInput=p.matcher(s);
if(mInput.find())
s=mInput.group(1); //Captured by parentheses
//Replace two or more spaces with a single space:
s=s.replaceAll(" {2,}"," ");
//Replace on or more spaces at the beginning of each line with no spaces.Must enable MULTILINE mode.
s=s.replaceAll("(?m)^+","");
System.out.println(s);
s=s.replaceFirst("[aeiou]","(VOWEL1)");
StringBuffer sbuf=new StringBuffer();
Pattern p1=Pattern.compile("[aeiou]");
Matcher m1=p1.matcher(s);
//Process the find information as you perform the replacements:
while(m1.find())
m1.appendReplacement(sbuf, m1.group().toUpperCase());
//Put in the remainder of the text:
m1.appendTail(sbuf);
System.out.println(sbuf);
}
}

五、reset()方法,可将现有的Matcher对象应用于一个新的字符序列。
例子:

package myfile;
import java.util.regex.*;
import java.io.*;
public class Resetting {

public static void main(String[] args) {
Matcher m=Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");
while(m.find())
System.out.println(m.group());
m.reset("fix the rug with bags");
while(m.find())
System.out.println(m.group());
}

}

六、在JDK1.4之前,将字符串分离成几部份的方法是:
利用StringTokenizer将该字符串“用标记断开”。
例子:

package myfile;
import java.util.*;
public class ReplacingStringTokenizer {
public static void main(String[] args) {
// TODO 自动生成方法存根
String input ="But I'm not dead yet! I feel happy!";
StringTokenizer stoke=new StringTokenizer(input);
while(stoke.hasMoreElements())
System.out.println(stoke.nextToken());
System.out.println(Arrays.asList(input.split(" ")));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: