设为首页 加入收藏

TOP

Java正则表达式示例
2015-08-31 21:24:05 来源: 作者: 【 】 浏览:35
Tags:Java 正则 表达式 示例

Java正则表达式示例:


import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegexMatches
{
? ? public static void main( String args[] ){


? ? ? // 按指定模式在字符串查找
? ? ? String line = "This order was placed for QT3000! OK?";
? ? ? String pattern = "(.*)(\\d+)(.*)";


? ? ? // 创建 Pattern 对象
? ? ? Pattern r = Pattern.compile(pattern);


? ? ? // 现在创建 matcher 对象
? ? ? Matcher m = r.matcher(line);
? ? ? if (m.find( )) {
? ? ? ? System.out.println("Found value: " + m.group(0) );
? ? ? ? System.out.println("Found value: " + m.group(1) );
? ? ? ? System.out.println("Found value: " + m.group(2) );
? ? ? } else {
? ? ? ? System.out.println("NO MATCH");
? ? ? }
? }
}


主要步骤:


通过正则表达式构建Pattern对象。
通过Pattern构建Matcher对象
调用Matcher.find()获得匹配结果


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C#获得当前截图 下一篇再说apue.h头文件(UNIX环境高级..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: