Search in sources :

Example 86 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project bboss by bbossgroups.

the class RegexUtil method isMatch.

/**
 * 检查src与正则表达式regex匹配结果
 * @param src String
 * @param regex String
 * @return boolean
 */
public static boolean isMatch(String src, String regex) {
    if (src == null)
        return false;
    boolean flag = false;
    String patternStr = regex;
    /**
     * 编译正则表达式patternStr,并用该表达式与传入的src字符串进行模式匹配,
     * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回
     * 该数组
     */
    PatternCompiler compiler = new Perl5Compiler();
    Pattern pattern = null;
    try {
        pattern = compiler.compile(patternStr, default_mask);
    } catch (MalformedPatternException e) {
        e.printStackTrace();
        return false;
    }
    PatternMatcher matcher = new Perl5Matcher();
    // log.debug("src:" + src);;
    // log.debug("pattern:" + pattern);
    // log.debug("regex:" + regex);
    flag = matcher.matches(src, pattern);
    return flag;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 87 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project bboss by bbossgroups.

the class RegexUtil method isContain.

public static boolean isContain(String src, String regex) {
    if (src == null)
        return false;
    String patternStr = regex;
    /**
     * 编译正则表达式patternStr,并用该表达式与传入的src字符串进行模式匹配,
     * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回
     * 该数组
     */
    PatternCompiler compiler = new Perl5Compiler();
    Pattern pattern = null;
    try {
        pattern = compiler.compile(patternStr, default_mask);
    } catch (MalformedPatternException e) {
        e.printStackTrace();
        return false;
    }
    PatternMatcher matcher = new Perl5Matcher();
    return matcher.contains(src, pattern);
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 88 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project bboss by bbossgroups.

the class RegexUtil method containWithPatternMatcherInput.

/**
 * 从src中提取包含模式regex的子字符串
 * @param src
 * @param regex
 * @return
 */
public static String[] containWithPatternMatcherInput(String src, String regex) {
    String patternStr = regex;
    /**
     * 编译正则表达式patternStr,并用该表达式与传入的src字符串进行模式匹配,
     * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回
     * 该数组
     */
    PatternCompiler compiler = new Perl5Compiler();
    Pattern pattern = null;
    try {
        pattern = compiler.compile(patternStr, default_mask);
    } catch (MalformedPatternException e) {
        e.printStackTrace();
        return null;
    }
    PatternMatcher matcher = new Perl5Matcher();
    MatchResult result = null;
    String[] tokens = null;
    List<String> sets = new ArrayList<String>();
    PatternMatcherInput input = new PatternMatcherInput(src);
    while (matcher.contains(input, pattern)) {
        result = matcher.getMatch();
        int size = result.groups();
        for (int i = 1; i < size; i++) {
            sets.add(result.group(i));
        }
    }
    tokens = new String[sets.size()];
    sets.toArray(tokens);
    return tokens;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 89 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project bboss by bbossgroups.

the class RegexUtil method contain.

/**
 * 从包含
 * @param src
 * @param regex
 * @return
 */
public static String[] contain(String src, String regex) {
    String patternStr = regex;
    /**
     * 编译正则表达式patternStr,并用该表达式与传入的src字符串进行模式匹配,
     * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回
     * 该数组
     */
    PatternCompiler compiler = new Perl5Compiler();
    Pattern pattern = null;
    try {
        pattern = compiler.compile(patternStr, default_mask);
    } catch (MalformedPatternException e) {
        e.printStackTrace();
        return null;
    }
    PatternMatcher matcher = new Perl5Matcher();
    MatchResult result = null;
    String[] tokens = null;
    if (matcher.contains(src, pattern)) {
        result = matcher.getMatch();
        int size = result.groups() - 1;
        tokens = new String[size];
        for (int i = 0; i < size; i++) {
            tokens[i] = result.group(i + 1);
        }
    }
    return tokens;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 90 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project bboss by bbossgroups.

the class TestInsertSqlParser method main.

public static void main(String[] args) throws MalformedPatternException {
    String insert = "Insert  into  oa_meetingpromptsound ( soundCode , soundName , soundFileName ) values ( '。.尹标平','bb','d()d' )";
    // String pattern = "(insert) \\t*(into) \\s*([a-z0-9A-Z_\\-]+)\\s*\\([a-z0-9A-Z_\\-]+\\s*,\\s*[a-z0-9A-Z_\\-]+)";
    // String pattern = "(insert)\\s+(into)\\s+([a-z0-9A-Z_\\-]+)\\s*(\\([^\\)]\\))\\s+([^\\s])";
    String patternStr = // 解析insert关键词
    "(insert)\\s+" + // 解析into关键词
    "(into)\\s+" + // 解析表名称
    "([^\\(]+)\\s*" + // 解析表字段
    "(\\([^\\)]+\\))\\s+" + // 解析value关键词
    "(values)\\s*" + // 解析字段值
    "(\\(.+)";
    PatternCompiler compiler = new Perl5Compiler();
    Pattern pattern = compiler.compile(patternStr, Perl5Compiler.CASE_INSENSITIVE_MASK);
    PatternMatcher matcher = new Perl5Matcher();
    MatchResult result = null;
    if (matcher.matches(insert.trim(), pattern)) {
        result = matcher.getMatch();
        System.out.println(result.group(1));
        System.out.println(result.group(2));
        System.out.println(result.group(3));
        System.out.println(result.group(4));
        System.out.println(result.group(5));
        System.out.println(result.group(6));
    }
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Aggregations

Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)102 Pattern (org.apache.oro.text.regex.Pattern)78 MatchResult (org.apache.oro.text.regex.MatchResult)39 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)38 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)37 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)32 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)22 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)20 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)8 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)7 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)7 MalformedURLException (java.net.MalformedURLException)5 PatternCacheLRU (org.apache.oro.text.PatternCacheLRU)5 PluginException (org.apache.wiki.api.exceptions.PluginException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 NoSuchElementException (java.util.NoSuchElementException)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2