Java Code Examples for org.apache.oro.text.regex.PatternMatcher#contains()

The following examples show how to use org.apache.oro.text.regex.PatternMatcher#contains() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegularUtil.java    From anyline with Apache License 2.0 6 votes vote down vote up
/** 
 * 字符串下标 regx在src中首次出现的位置 
 * @param src     src
 * @param regx    regx
 * @param begin   有效开始位置 
 * @return return
 */ 
public static int indexOf(String src, String regx, int begin){ 
	int idx = -1; 
	try{ 
		PatternCompiler patternCompiler = new Perl5Compiler(); 
		Pattern pattern = patternCompiler.compile(regx, Perl5Compiler.CASE_INSENSITIVE_MASK); 
		PatternMatcher matcher = new Perl5Matcher(); 
		PatternMatcherInput input = new PatternMatcherInput(src); 
		 
		while(matcher.contains(input, pattern)){ 
			MatchResult matchResult = matcher.getMatch(); 
			int tmp = matchResult.beginOffset(0); 
			if(tmp >= begin){//匹配位置从begin开始 
				idx = tmp; 
				break; 
			} 
		} 
	}catch(Exception e){
		log.error("[提取异常][src:{}][regx:{}]", src, regx); 
		e.printStackTrace();
	} 
	return idx; 
}
 
Example 2
Source File: RegularContain.java    From anyline with Apache License 2.0 6 votes vote down vote up
/** 
 * 提取子串 
 * @param src	输入字符串  src	输入字符串
 * @param regx	表达式  regx	表达式
 * @return return
 */ 
public List<List<String>> fetchs(String src, String regx){ 
	List<List<String>> list = new ArrayList<List<String>>(); 
	try{ 
		Pattern pattern = patternCompiler.compile(regx, Perl5Compiler.CASE_INSENSITIVE_MASK); 
		PatternMatcher matcher = new Perl5Matcher(); 
		PatternMatcherInput input = new PatternMatcherInput(src); 
		while(matcher.contains(input, pattern)){ 
			MatchResult matchResult = matcher.getMatch(); 
			int groups = matchResult.groups(); 
			List<String> item = new ArrayList<String>(); 
			for(int i=0; i<groups; i++){ 
				item.add(matchResult.group(i)); 
			} 
			list.add(item); 
		} 
	}catch(Exception e){ 
		log.error("[提取异常][src:{}][reg:{}]", src, regx);
		if(ConfigTable.isDebug() && log.isWarnEnabled()){
			e.printStackTrace();
		} 
	} 
	return list; 
}
 
Example 3
Source File: RegularContain.java    From anyline with Apache License 2.0 6 votes vote down vote up
/** 
 * 提取子串 
 * @param src		输入字符串  src		输入字符串
 * @param regx		表达式  regx		表达式
 * @param idx		指定提取位置  idx		指定提取位置
 * @return return
 */ 
public List<String> fetch(String src, String regx, int idx) throws Exception{ 
	List<String> list = new ArrayList<String>(); 
	 
	try{ 
		Pattern pattern = patternCompiler.compile(regx, Perl5Compiler.CASE_INSENSITIVE_MASK); 
		PatternMatcher matcher = new Perl5Matcher(); 
		PatternMatcherInput input = new PatternMatcherInput(src); 
		 
		while(matcher.contains(input, pattern)){ 
			MatchResult matchResult = matcher.getMatch(); 
			list.add(matchResult.group(idx)); 
		} 
	}catch(Exception e){
		log.error("[提取异常][src:{}][reg:{}]", src, regx);
		if(ConfigTable.isDebug() && log.isWarnEnabled()){
			e.printStackTrace();
		} 
		throw e; 
	} 
	return list; 
}
 
Example 4
Source File: RegxpContain.java    From anyline with Apache License 2.0 6 votes vote down vote up
/** 
 * 提取子串 
 * @param src	输入字符串  src	输入字符串
 * @param regx	表达式  regx	表达式
 * @return return
 */ 
public List<List<String>> fetchs(String src, String regx){ 
	List<List<String>> list = new ArrayList<List<String>>(); 
	try{ 
		Pattern pattern = patternCompiler.compile(regx, Perl5Compiler.CASE_INSENSITIVE_MASK); 
		PatternMatcher matcher = new Perl5Matcher(); 
		PatternMatcherInput input = new PatternMatcherInput(src); 
		while(matcher.contains(input, pattern)){ 
			MatchResult matchResult = matcher.getMatch(); 
			int groups = matchResult.groups(); 
			List<String> item = new ArrayList<String>(); 
			for(int i=0; i<groups; i++){ 
				item.add(matchResult.group(i)); 
			} 
			list.add(item); 
		} 
	}catch(Exception e){
		log.error("[提取异常][src:{}][reg:{}]", src, regx);
		e.printStackTrace(); 
	} 
	return list; 
}
 
Example 5
Source File: RegxpContain.java    From anyline with Apache License 2.0 6 votes vote down vote up
/** 
 * 提取子串 
 * @param src		输入字符串  src		输入字符串
 * @param regx		表达式  regx		表达式
 * @param idx		指定提取位置  idx		指定提取位置
 * @return return
 */ 
public List<String> fetch(String src, String regx, int idx) throws Exception{ 
	List<String> list = new ArrayList<String>(); 
	 
	try{ 
		Pattern pattern = patternCompiler.compile(regx, Perl5Compiler.CASE_INSENSITIVE_MASK); 
		PatternMatcher matcher = new Perl5Matcher(); 
		PatternMatcherInput input = new PatternMatcherInput(src); 
		 
		while(matcher.contains(input, pattern)){ 
			MatchResult matchResult = matcher.getMatch(); 
			list.add(matchResult.group(idx)); 
		} 
	}catch(Exception e){ 
		log.error("[提取异常][src:{}][reg:{}]", src, regx);
		e.printStackTrace(); 
		throw e; 
	} 
	return list; 
}
 
Example 6
Source File: RegularContain.java    From anyline with Apache License 2.0 5 votes vote down vote up
/** 
 * 配置状态 
 * @param src  src
 * @param regx  regx
 * @return return
 */ 
public boolean match(String src, String regx){ 
	boolean result = false; 
	try{ 
		Pattern pattern = patternCompiler.compile(regx, Perl5Compiler.CASE_INSENSITIVE_MASK); 
		PatternMatcher matcher = new Perl5Matcher(); 
		result = matcher.contains(src, pattern); 
	}catch(Exception e){ 
		result = false;
		if(ConfigTable.isDebug() && log.isWarnEnabled()){
			e.printStackTrace();
		} 
	} 
	return result; 
}
 
Example 7
Source File: RegxpContain.java    From anyline with Apache License 2.0 5 votes vote down vote up
/** 
 * 匹配状态 
 * @param src  src
 * @param regx 表达式
 * @return return
 */ 
public boolean match(String src, String regx){ 
	boolean result = false; 
	try{ 
		Pattern pattern = patternCompiler.compile(regx, Perl5Compiler.CASE_INSENSITIVE_MASK); 
		PatternMatcher matcher = new Perl5Matcher(); 
		result = matcher.contains(src, pattern); 
	}catch(Exception e){ 
		result = false; 
	} 
	return result; 
}
 
Example 8
Source File: UtilHttp.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * checks, if the current request comes from a searchbot
 *
 * @param request
 * @return whether the request is from a web searchbot
 */
public static boolean checkURLforSpiders(HttpServletRequest request) {
    boolean result = false;

    String spiderRequest = (String) request.getAttribute("_REQUEST_FROM_SPIDER_");
    if (UtilValidate.isNotEmpty(spiderRequest)) {
        if ("Y".equals(spiderRequest)) {
            return true;
        }
        return false;
    }
    String initialUserAgent = request.getHeader("User-Agent") != null ? request.getHeader("User-Agent") : "";
    List<String> spiderList = StringUtil.split(UtilProperties.getPropertyValue("url", "link.remove_lsessionid.user_agent_list"), ",");

    if (UtilValidate.isNotEmpty(spiderList)) {
        for (String spiderNameElement : spiderList) {
            Pattern pattern = null;
            try {
                pattern = PatternFactory.createOrGetPerl5CompiledPattern(spiderNameElement, false);
            } catch (MalformedPatternException e) {
                Debug.logError(e, module);
            }
            PatternMatcher matcher = new Perl5Matcher();
            if (matcher.contains(initialUserAgent, pattern)) {
                request.setAttribute("_REQUEST_FROM_SPIDER_", "Y");
                result = true;
                break;
            }
        }
    }

    if (!result) {
        request.setAttribute("_REQUEST_FROM_SPIDER_", "N");
    }

    return result;
}
 
Example 9
Source File: JavaSource.java    From yugong with GNU General Public License v2.0 5 votes vote down vote up
public String findFirst(String originalStr, String regex) {
    if (StringUtils.isBlank(originalStr) || StringUtils.isBlank(regex)) {
        return StringUtils.EMPTY;
    }

    PatternMatcher matcher = new Perl5Matcher();
    if (matcher.contains(originalStr, patterns.get(regex))) {
        return StringUtils.trimToEmpty(matcher.getMatch().group(0));
    }
    return StringUtils.EMPTY;
}
 
Example 10
Source File: RegexpStressTest.java    From common-utils with GNU General Public License v2.0 5 votes vote down vote up
public void run(int loop) {
    PatternMatcher matcher = new Perl5Matcher();

    for (int i = 0; i < loop; i++) {
        for (Pattern pattern : patterns) {
            for (String value : data) {
                matcher.contains(value, pattern);
            }
        }
    }
}
 
Example 11
Source File: RegexpStressTest.java    From common-utils with GNU General Public License v2.0 5 votes vote down vote up
public void warmUp() {
    PatternMatcher matcher = new Perl5Matcher();
    int matchedCount = 0;

    for (Pattern pattern : patterns) {
        for (String value : data) {
            if (matcher.contains(value, pattern)) {
                matchedCount++;
            }
        }
    }

    assertEquals(RegexpStressTest.this.matchedCount, matchedCount);
}
 
Example 12
Source File: OutlinkExtractor.java    From anthelion with Apache License 2.0 4 votes vote down vote up
/**
 * Extracts <code>Outlink</code> from given plain text and adds anchor
 * to the extracted <code>Outlink</code>s
 * 
 * @param plainText the plain text from wich URLs should be extracted.
 * @param anchor    the anchor of the url
 * 
 * @return Array of <code>Outlink</code>s within found in plainText
 */
public static Outlink[] getOutlinks(final String plainText, String anchor, Configuration conf) {
  long start = System.currentTimeMillis();
  final List<Outlink> outlinks = new ArrayList<Outlink>();

  try {
    final PatternCompiler cp = new Perl5Compiler();
    final Pattern pattern = cp.compile(URL_PATTERN,
        Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK
            | Perl5Compiler.MULTILINE_MASK);
    final PatternMatcher matcher = new Perl5Matcher();

    final PatternMatcherInput input = new PatternMatcherInput(plainText);

    MatchResult result;
    String url;

    //loop the matches
    while (matcher.contains(input, pattern)) {
      // if this is taking too long, stop matching
      //   (SHOULD really check cpu time used so that heavily loaded systems
      //   do not unnecessarily hit this limit.)
      if (System.currentTimeMillis() - start >= 60000L) {
        if (LOG.isWarnEnabled()) {
          LOG.warn("Time limit exceeded for getOutLinks");
        }
        break;
      }
      result = matcher.getMatch();
      url = result.group(0);
      try {
        outlinks.add(new Outlink(url, anchor));
      } catch (MalformedURLException mue) {
        LOG.warn("Invalid url: '" + url + "', skipping.");
      }
    }
  } catch (Exception ex) {
    // if the matcher fails (perhaps a malformed URL) we just log it and move on
    if (LOG.isErrorEnabled()) { LOG.error("getOutlinks", ex); }
  }

  final Outlink[] retval;

  //create array of the Outlinks
  if (outlinks != null && outlinks.size() > 0) {
    retval = outlinks.toArray(new Outlink[0]);
  } else {
    retval = new Outlink[0];
  }

  return retval;
}
 
Example 13
Source File: reMatch.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	String regexp = getNamedStringParam(argStruct, "regular", "");
	String strToSearch = getNamedStringParam(argStruct, "string", "");
	boolean bUnique = getNamedBooleanParam(argStruct, "unique",false);
	
	HashSet<String>	uniqueTrack = null;
	if ( bUnique ){
		uniqueTrack	= new HashSet<String>();
	}


	/* Setup the RegEx */
	PatternCompiler compiler = new Perl5Compiler();
	Pattern pattern;
	
	try {
		if (caseSensitiveMatch) {
			pattern = compiler.compile(regexp, Perl5Compiler.SINGLELINE_MASK);
		} else {
			pattern = compiler.compile(regexp, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK);
		}
	} catch (MalformedPatternException e) {
		cfCatchData catchD = new cfCatchData();
		catchD.setType("Function");
		catchD.setMessage("REMatch - invalid parameter");
		catchD.setDetail("Invalid regular expression ( " + regexp + " )");
		throw new cfmRunTimeException(catchD);
	}

	/* Perform the search */
	cfArrayData	array	= cfArrayData.createArray(1);
	PatternMatcher matcher = new Perl5Matcher();
	MatchResult result;
	PatternMatcherInput input = new PatternMatcherInput( strToSearch );
	while ( matcher.contains(input, pattern) ) {
		result = matcher.getMatch();
		
		String strResult = result.toString();
		if ( bUnique ){
			if ( !uniqueTrack.contains( strResult ) ){
				array.addElement( new cfStringData( strResult ) );
				uniqueTrack.add( strResult );
			}
		}else		
			array.addElement( new cfStringData( strResult ) );
	}
	
	return array;
}
 
Example 14
Source File: OutlinkExtractor.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Extracts <code>Outlink</code> from given plain text and adds anchor
 * to the extracted <code>Outlink</code>s
 * 
 * @param plainText the plain text from wich URLs should be extracted.
 * @param anchor    the anchor of the url
 * 
 * @return Array of <code>Outlink</code>s within found in plainText
 */
public static Outlink[] getOutlinks(final String plainText, String anchor, Configuration conf) {
  long start = System.currentTimeMillis();
  final List<Outlink> outlinks = new ArrayList<Outlink>();

  try {
    final PatternCompiler cp = new Perl5Compiler();
    final Pattern pattern = cp.compile(URL_PATTERN,
        Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK
            | Perl5Compiler.MULTILINE_MASK);
    final PatternMatcher matcher = new Perl5Matcher();

    final PatternMatcherInput input = new PatternMatcherInput(plainText);

    MatchResult result;
    String url;

    //loop the matches
    while (matcher.contains(input, pattern)) {
      // if this is taking too long, stop matching
      //   (SHOULD really check cpu time used so that heavily loaded systems
      //   do not unnecessarily hit this limit.)
      if (System.currentTimeMillis() - start >= 60000L) {
        if (LOG.isWarnEnabled()) {
          LOG.warn("Time limit exceeded for getOutLinks");
        }
        break;
      }
      result = matcher.getMatch();
      url = result.group(0);
      try {
        outlinks.add(new Outlink(url, anchor));
      } catch (MalformedURLException mue) {
        LOG.warn("Invalid url: '" + url + "', skipping.");
      }
    }
  } catch (Exception ex) {
    // if the matcher fails (perhaps a malformed URL) we just log it and move on
    if (LOG.isErrorEnabled()) { LOG.error("getOutlinks", ex); }
  }

  final Outlink[] retval;

  //create array of the Outlinks
  if (outlinks != null && outlinks.size() > 0) {
    retval = outlinks.toArray(new Outlink[0]);
  } else {
    retval = new Outlink[0];
  }

  return retval;
}