正则的java中的三种应用

写了正则的介绍好长时间,不动了。看到有人写了写关于正则的java应用,毕竟我们这里是javaResearch :)。所以写了些代码,为了简单所以什么也没有使用,只要考到本地就可以看到输出。

下面是代码:大家应该都看得懂,全当是自己记录。

   package simpleTest;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
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;

import org.apache.regexp.RE;

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


/**
 * @author Edgar(wxt)
 *
 */
public class TestForRex {
    static String rexPatternString = "^((http|https)://[^/]*/).*";
    static String rexSearchString = "http://10.4.8.43/makdsm/amd.do";

    /**
     * @param args
     */
    public static void main(String[] args) {
        regexTestPrint();
        rexUtilTestForPrint();
        oroTestForPrint();
    }

    /**
     * this is to test the regex whici is supplied by the jdk1.4
     *
     */
    public static void regexTestPrint() {
        Matcher testPreMatcher = Pattern.compile(rexPatternString)
                                        .matcher(rexSearchString);
        int groutCount = testPreMatcher.groupCount();
        System.out.println(
            "this is to print the group which is set by parenthesis :" +
            groutCount);

        if (testPreMatcher.find()) {
            for (int i = 0; i < (groutCount + 1); i++) {
                System.out.println(
                    "this is to print which the search of the group for the pattern " +
                    i + ":" + testPreMatcher.group(i));
            }
        }
    }

    /**
     * This is to test fro print of the appache of rexUtil
     *
     */
    public static void rexUtilTestForPrint() {
        RE re = new RE(rexPatternString);

        boolean isTheRegMatch = re.match(rexSearchString);

        if (isTheRegMatch) {
            int matchCount = re.getParenCount();
            System.out.println(
                "This is to print which the search of the parenthesis count :" +
                matchCount);

            for (int mc = 0; mc < matchCount; mc++) {
                System.out.println("The match content level is i=" + mc + ":" +
                    re.getParen(mc));
            }
        }
    }

    /**
     * this is to test for the oro which appache supllying of regular
     *
     */
    public static void oroTestForPrint() {
        PatternMatcher patternMatcher = new Perl5Matcher();
        PatternCompiler patternCompiler = new Perl5Compiler();
        org.apache.oro.text.regex.Pattern pattern;
        MatchResult matchResult;
        PatternMatcherInput patternMatcherInput;

        try {
            pattern = patternCompiler.compile(rexPatternString);
        } catch (MalformedPatternException e) {
            System.err.println("Bad pattern.");
            System.err.println(e.getMessage());

            return;
        }

        patternMatcherInput = new PatternMatcherInput(rexSearchString);

        while (patternMatcher.contains(patternMatcherInput, pattern)) {
            matchResult = patternMatcher.getMatch();
            System.out.println("oro Result is group count :" +
                matchResult.groups());

            for (int k = 0; k < matchResult.groups(); k++) {
                System.out.println("oro group Result fattern is " + k + "=:" +
                    matchResult.group(k));
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值