apache jakarta oro 常用例子(带注释)

package test;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.MalformedPatternException;
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;
import org.apache.oro.text.regex.Perl5Substitution;
import org.apache.oro.text.regex.Util;
 
/**
 * apache oro正则表达式学习
 *
 * @author xiaofeng
 *
 * @create 2007-5-21上午11:42:36
 */
public class RegexStudy {
    private static final Log log = LogFactory.getLog(RegexStudy.class);
    private PatternCompiler pc = new Perl5Compiler();
    private PatternMatcher matcher = new Perl5Matcher();
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        RegexStudy ps = new RegexStudy();
        ps.substitutePatternTest();
    }
 
    /**
     * 精确匹配表达式
     */
    public void simplePatternTest() {
        Pattern p = null;
 
        //编译一下,生成表达式
        try {
            p = pc.compile("<(([a-z]{2})|([0-9]{2}))>.*", Perl5Compiler.CASE_INSENSITIVE_MASK);
        } catch (MalformedPatternException e) {
            log.error("regex compile error : ", e);
        }
 
        if (null != p) {
            //查看字符串中是否完全匹配表达式p
            if (matcher.matches("<111>adsf", p)) {
                log.info("matched");
            } else {
                log.info("not matched");
            }
        }
    }
 
    /**
     * 部分匹配表达式
     */
    public void containsPatternTest() {
        Pattern p = null;
 
        //编译一下,生成表达式
        try {
            p = pc.compile("<(([a-z]{2})|([0-9]{2}))>.*", Perl5Compiler.CASE_INSENSITIVE_MASK);
        } catch (MalformedPatternException e) {
            log.error("regex compile error : ", e);
        }
 
        if (null != p) {
            //查看字符串中是否部分匹配表达式p
            if (matcher.contains("<11>adsf", p)) {
                log.info("matched");
                //得到匹配结果
                MatchResult mr = matcher.getMatch();
                //输出匹配的第一个子表达式值,从1开始的
                log.info(mr.group(1));
            } else {
                log.info("not matched");
            }
        }
    }
 
    /**
     * 根据匹配规则,替换子表达式
     */
    public void substitutePatternTest() {
        Pattern p = null;
 
        //编译一下,生成表达式
        try {
            p = pc.compile("<(([a-z]{2})|([0-9]{2}))>.*", Perl5Compiler.CASE_INSENSITIVE_MASK);
        } catch (MalformedPatternException e) {
            log.error("regex compile error : ", e);
        }
 
        if (null != p) {
            //得到匹配过后,替换掉子表达式值后的字符串
            String result = Util.substitute(
                    matcher,
                    p,
                    new Perl5Substitution("<$1>adsf"), //替换表达式
                    "<11>adsf", //匹配的字符串
                    Util.SUBSTITUTE_ALL //置换所有的
                );
            log.info(result);
        }
    }
 
}

转载请注明原文链接:http://kenwublog.com/apache-jakarta-oro-example

2007/05/21 | Posted in Development Skills
No comments yet.

Leave a comment

Attention: Java syntax highlighting is enable. For example: <pre lang="java"> class A {} </pre>