`
ldb19890624
  • 浏览: 230478 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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(" ")));
}

}

分享到:
评论

相关推荐

    CSS2.0-CSS3.0-HTML5-JavaScript-JDK1.8-正则表达式,帮助文档CHM

    CSS2.0-CSS3.0-HTML5-JavaScript-JDK1.8-正则表达式,全中文帮助文档,全都是CHM版 里面包含15个CHM文件,其中有六大类,有的有多个版本全是中文版 CSS2.0就标准的一个版本,够用了 CSS3.0有P零雾雨版,ISD版还有,...

    jdk-7u80-windows-x64安装包

    jdk-7u80-windows-x64安装包 jdk-7u80-windows-x64安装包 jdk-7u80-windows-x64安装包 jdk-7u80-windows-x64安装包 jdk-7u80-windows-x64安装包 jdk-7u80-windows-x64安装包 jdk-7u80-windows-x64安装包 jdk-7u80-...

    开发工具 jdk-8u121-windows-i586

    开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8...

    bcprov-jdk15to18-1.69.jar

    bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-1.69.jar bcprov-jdk15to18-...

    jdk-6u39-windows-i586

    jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows...

    常用java正则表达式

    本文写作时,一个包含了用正则表达式进行文本处理的Java规范需求(Specification Request)已经得到认可,你可以期待在JDK的下一版本中看到它。 然而,如果现在就需要使用正则表达式,又该怎么办呢?你可以从Apache...

    jdk-6u21-windows-x64

    jdk-6u21-windows-x64 jdk-6u21-windows-x64 jdk-6u21-windows-x64 jdk-6u21-windows-x64 jdk-6u21-windows-x64 jdk-6u21-windows-x64 jdk-6u21-windows-x64 jdk-6u21-windows-x64 jdk-6u21-windows-x64 jdk-6u21-...

    java JDK1.8.60-jdk-8u60-windows-x64

    java JDK1.8.60-jdk-8u60-windows-x64,java JDK1.8.60-jdk-8u60-windows-x64,java JDK1.8.60-jdk-8u60-windows-x64

    jdk 6.0 jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008(微信开发平台开发JDK)

    微信开发平台开发工具 JDK jdk 6.0 jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008

    jdk-8u60-windows-i586-JDK1.8-32位

    jdk-8u60-windows-i586 jdk-8u60-windows-i586 jdk-8u60-windows-i586 jdk-8u60-windows-i586 jdk-8u60-windows-i586

    jdk-6u2-windows-i586-p

    jdk-6u2-windows-i586-p

    bcmail-jdk14-1.38-API文档-中文版.zip

    赠送jar包:bcmail-jdk14-1.38.jar; 赠送原API文档:bcmail-jdk14-1.38-javadoc.jar; 赠送源代码:bcmail-jdk14-1.38-sources.jar; 赠送Maven依赖信息文件:bcmail-jdk14-1.38.pom; 包含翻译后的API文档:bcmail...

    jdk-8u231-windows-x64.exe 安装版

    文件名称:C:\Users\LBWNB\Desktop\jdk-8u231-windows-x64\jdk-8u231-windows-x64.exe 文件大小:220392992 字节 文件版本:8.0.2310.11 修改时间:2019年11月11日 13:31:03 MD5 :F8A35AB83D651DC52CC77B19CD818167...

    jdk-6u14-linux-x64-rpm.bin

    jdk-6u14-linux-x64-rpm.bin

    jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe

    jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe java编译环境

    jdk-6u14-linux-x64.bin

    jdk-6u14-linux-x64.bin

    jdk-6u20-windows-i586(官方下载地址)

    最新版本的jdk欢迎大家来下载!最新版本的jdk欢迎大家来下载!

    jdk-6u7-windows-i586-p.exe

    jdk-6u7-windows-i586-p.exe jdk1.6

    JDK-7u80-windows-x64版本

    JDK版本:JDK-7u80-windows-x64版本,即为jdk1.7,此版本会自动安装JRE(Java的一个运行环境).

    腾讯地图jdk qqmap-wx-jssdk

    腾讯地图jdk qqmap-wx-jssdk 腾讯地图jdk qqmap-wx-jssdk 腾讯地图jdk qqmap-wx-jssdk 腾讯地图jdk qqmap-wx-jssdk 腾讯地图jdk qqmap-wx-jssdk 腾讯地图jdk qqmap-wx-jssdk 腾讯地图jdk qqmap-wx-jssdk 腾讯地图jdk...

Global site tag (gtag.js) - Google Analytics