Java org.apache.oro.text.regex.Perl5Compiler 代码实例

・7 分钟阅读

以下是展示如何使用org.apache.oro.text.regex.Perl5Compiler的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


@Override
public Object visit(StringBinaryComparison n, Void arg) {
        String first = (String) n.getLeftOperand().accept(this, null);
        String second = (String) n.getRightOperand().accept(this, null);
        Operator op = n.getOperator();
        switch (op) {
        case EQUALSIGNORECASE:
                return first.equalsIgnoreCase(second) ? TRUE_VALUE : FALSE_VALUE;
        case EQUALS:
                return first.equals(second) ? TRUE_VALUE : FALSE_VALUE;
        case ENDSWITH:
                return first.endsWith(second) ? TRUE_VALUE : FALSE_VALUE;
        case CONTAINS:
                return first.contains(second) ? TRUE_VALUE : FALSE_VALUE;
        case PATTERNMATCHES:
                return second.matches(first) ? TRUE_VALUE : FALSE_VALUE;
        case APACHE_ORO_PATTERN_MATCHES: {
                Perl5Matcher matcher = new Perl5Matcher();
                Perl5Compiler compiler = new Perl5Compiler();
                Pattern pattern;
                try {
                        pattern = compiler.compile(first);
                } catch (MalformedPatternException e) {
                        throw new RuntimeException(e);
                }
                return matcher.matches(second, pattern) ? TRUE_VALUE : FALSE_VALUE;
        }
        default:
                log.warn("StringComparison: unimplemented operator!" + op);
                return null;
        }
}
 

实例 2


@Override
public Pattern compilePattern(String patternStr) throws MalformedPatternException {
    Perl5Compiler compiler = getCompiler();
    // adding case insensitivity mask
    return compiler.compile(patternStr, Perl5Compiler.DEFAULT_MASK
            | Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK);
}
 

实例 3


/**
 * @param regex
 *          the regular expression pattern to compile.
 * @throws PatternInvalidSyntaxException
 *           if the regular expression's syntax is invalid.
 */
public Perl5Regex(String regex) {
  super();
  Perl5Compiler perl5Compiler = new Perl5Compiler();
  try {
    pattern = perl5Compiler.compile(regex);
    regexPattern = regex;
  } catch (MalformedPatternException malformedPatternException) {
    throw new PatternInvalidSyntaxException(malformedPatternException
        .getMessage());
  }
}
 

实例 4


/**
 * Get all substrings that match the given regular expression
 * @param sSource String Source
 * @param sRegExp String Regular Expression
 * @return ArrayList<MatchResult>
 * @throws MalformedPatternException
 * @since v7.0
 */
public static ArrayList<MatchResult> getAllMatches (String sSource, String sRegExp) throws MalformedPatternException {
  ArrayList<MatchResult> aRetVal = new ArrayList<MatchResult>();
  if (sSource!=null && sRegExp!=null) {
    if (null==oMatcher) oMatcher = new Perl5Matcher();
    if (null==oCompiler) oCompiler = new Perl5Compiler();
    Perl5Pattern oPatt = (Perl5Pattern) oCompiler.compile(sRegExp);
    PatternMatcherInput oPmin = new PatternMatcherInput(sSource);
    while (oMatcher.contains(oPmin, oPatt)) {
      aRetVal.add(oMatcher.getMatch());
    } // wend
  } // fi
  return aRetVal;
}
 

实例 5


public static void addRegex(Collection list, String regex) {
    try {
        Perl5Compiler pc = new Perl5Compiler();
        Pattern pattern = pc.compile(regex);
        list.add(pattern);
    } catch (MalformedPatternException e) {
        logger.warn("The regular expression " + regex + " is invalid: "
                + e.getLocalizedMessage());
    }
}
 

实例 6


@Override
public Pattern compilePattern(String patternStr)
        throws MalformedPatternException {
    Perl5Compiler compiler = getCompiler();
    // adding case insensitivity mask
    return compiler.compile(patternStr, Perl5Compiler.DEFAULT_MASK|Perl5Compiler.SINGLELINE_MASK|Perl5Compiler.CASE_INSENSITIVE_MASK);
}
 

实例 7


/**
 * ??????Pattern?UrlType
 * 
 * @param perl5RegExp
 * @return
 */
private URLPatternHolder convertStringToPattern(String line) {
    URLPatternHolder holder = new URLPatternHolder();
    Pattern compiledPattern;
    Perl5Compiler compiler = new Perl5Compiler();
    String perl5RegExp = line;
    try {
        compiledPattern = compiler.compile(perl5RegExp, Perl5Compiler.READ_ONLY_MASK);
        holder.setCompiledPattern(compiledPattern);
    } catch (MalformedPatternException mpe) {
        throw new IllegalArgumentException("Malformed regular expression: " + perl5RegExp);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Added regular expression: " + compiledPattern.getPattern().toString());
    }
    return holder;
}
 

实例 8


boolean execLike( cfData _str1, cfData _str2 ) throws cfmRunTimeException{
        String strToSearch = _str1.getString();
                String likeString = getLikeExpression( _str2 );
        PatternMatcher matcher;
                PatternCompiler compiler;
                Pattern pattern;
                PatternMatcherInput input;
                compiler = new Perl5Compiler();
                matcher  = new Perl5Matcher();
        try {
                pattern = compiler.compile( likeString );
                } catch( MalformedPatternException e ) {
                return false;
        }
        input   = new PatternMatcherInput( strToSearch );
        if( matcher.matches( input, pattern ) ) {
                return true;
        }else{
                return false;
        }
}
 
讨论
淘淘あ西西 profile image