文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Java正则式的相关实例(1)—使用第..

Java正则式的相关实例(1)—使用第..

时间:2010-09-11  来源:王守饼

关于正则式的概念、作用以及基础知识,这里就不多讲啦,这里给出两类实例供大家参考。其一是使用第三方包,其二是使用java API进行操作。本篇先给出第一类的实例,这里使用jakarta-oro.jar,这个包大家可以到网上down,然后添加到过程中即可。

*********************************************************************************************

//1、匹配服务器日志里德IP和时间

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hfut.wst.regExp;

import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

/**
 *
 * @author Administrator
 */
public class regularExpressionTest {

    public static void main(String[] args) {
        String logEntry = "192.168.1.1 - - [11/September/2010:10:17:30 -0500]"
                + "\"GET /IsAlive.htm HTTP/1.0\" 200 15";
        String regExp = "([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})\\s-\\s-\\s\\[([^\\]]+)\\]";
        try {
            PatternCompiler compiler = new Perl5Compiler();
            Pattern pattern = compiler.compile(regExp);
            PatternMatcher matcher = new Perl5Matcher();
            if(matcher.contains(logEntry, pattern)){
                MatchResult result = matcher.getMatch();
                System.out.println("    IP:"+result.group(1));
                System.out.println("  Date:"+result.group(2));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

      
    }
}
*********************************************************************************************

 

//匹配html font标签及其属性

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hfut.wst.regExp;

import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

/**
 *
 * @author Administrator
 */
public class regExpForHtmlFont {

    public static void main(String[] args) {
        String html = "<font face=\"Arial, Serif\" size=\"+2\" color=\"red\">";
        String regExpForTag = "<

相关阅读 更多 +
排行榜 更多 +
翌日波奇狗的历险记手机版下载

翌日波奇狗的历险记手机版下载

休闲益智 下载
怪兽远征安卓版下载

怪兽远征安卓版下载

角色扮演 下载
谷歌卫星地图免费版下载

谷歌卫星地图免费版下载

生活实用 下载