字符串操作(2)---使用正则表达式
2007-02-14 15:21
288 查看
/*********************************************
*Author:Java619
*Time:2007-02-14
**********************************************/
本文继续字符串操作(1)中的问题(从"aab${aaa}esfd${bb}1c${cccc}132${dds}"中分离出aaa,bb,dds),这次将使用正则表达式,以使上一例子更通用.本例用于了Jakarta ORO 包(点击下载或到apache官方网下载).关于正表达式的介绍大家可以去看"仙人掌工作室"中的介绍,里面已经说得很祥细.
要从"aab${aaa}esfd${bb}1c${cccc}132${dds}"中分离出aaa,bb,dds可以这样实现
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.Perl5Pattern;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.MatchResult;
public class RegularExpress3
{
/**
*从"aab${aaa}esfd${bb}1c${cccc}132${dds}"中分离出
*aaa,bb,cccc,dds
*@author java619
*/
public static void main(String[] args)
{
String str="aab${aaa}esfd${bb}1c${cccc}132${dds}";
////$ 使用转意符表示$ 注意由于BLOG显示问题,记得是两个"/"
////{ //} 使用转意符表示{ } 注意"{","}"在正则表达式中是有特殊意义的
//所以应用"/{"加以区分,而第一个"/"用天告诉Java其后要表示一"/"
String regexp="//$//{([^//}]+)//}";
try{
PatternCompiler compile=new Perl5Compiler();
Pattern pattern=compile.compile(regexp);
PatternMatcher matcher=new Perl5Matcher();
PatternMatcherInput input=new PatternMatcherInput(str);
while(matcher.contains(input,pattern)){
MatchResult result=matcher.getMatch();
System.out.println(result.group(1));
}
}catch(Exception e){
System.out.println(e.toString());
}
}
}
*Author:Java619
*Time:2007-02-14
**********************************************/
本文继续字符串操作(1)中的问题(从"aab${aaa}esfd${bb}1c${cccc}132${dds}"中分离出aaa,bb,dds),这次将使用正则表达式,以使上一例子更通用.本例用于了Jakarta ORO 包(点击下载或到apache官方网下载).关于正表达式的介绍大家可以去看"仙人掌工作室"中的介绍,里面已经说得很祥细.
要从"aab${aaa}esfd${bb}1c${cccc}132${dds}"中分离出aaa,bb,dds可以这样实现
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.Perl5Pattern;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.MatchResult;
public class RegularExpress3
{
/**
*从"aab${aaa}esfd${bb}1c${cccc}132${dds}"中分离出
*aaa,bb,cccc,dds
*@author java619
*/
public static void main(String[] args)
{
String str="aab${aaa}esfd${bb}1c${cccc}132${dds}";
////$ 使用转意符表示$ 注意由于BLOG显示问题,记得是两个"/"
////{ //} 使用转意符表示{ } 注意"{","}"在正则表达式中是有特殊意义的
//所以应用"/{"加以区分,而第一个"/"用天告诉Java其后要表示一"/"
String regexp="//$//{([^//}]+)//}";
try{
PatternCompiler compile=new Perl5Compiler();
Pattern pattern=compile.compile(regexp);
PatternMatcher matcher=new Perl5Matcher();
PatternMatcherInput input=new PatternMatcherInput(str);
while(matcher.contains(input,pattern)){
MatchResult result=matcher.getMatch();
System.out.println(result.group(1));
}
}catch(Exception e){
System.out.println(e.toString());
}
}
}
相关文章推荐
- 4种使用javascript正则表达式进行字符串replace操作的方法
- 提高你的Java代码质量吧:推荐在复杂字符串操作中使用正则表达式
- 提高你的Java代码质量吧:推荐在复杂字符串操作中使用正则表达式
- Java使用正则表达式及字符串操作,抽取网页信息
- Java_正则表达式_推荐在复杂字符串操作中使用正则表达式
- [编写高质量代码:改善java程序的151个建议]建议57 推荐在复杂字符串操作中使用正则表达式
- [改善Java代码]推荐在复杂字符串操作中使用正则表达式
- 提高你的Java代码质量吧:推荐在复杂字符串操作中使用正则表达式 .
- 使用 preg_split 正则表达式截取字符串
- java 使用正则表达式统计一个字符串中子字符串出现次数
- php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
- 使用正则表达式操作html标签中的特定属性
- python中使用正则表达式匹配出字符串中的数字
- IOS--正则表达式的使用(字符串的查找和替换)
- 详解Ruby中正则表达式对字符串的匹配和替换操作
- 第三章 字符串操作与正则表达式
- java使用正则表达式获取字符串中的数字
- 14.05 - 使用正则表达式分割字符串
- Python使用中文正则表达式匹配指定中文字符串的方法示例