Java中正则表达式
Jakarta ORO一套文本处理工具,能提供perl5.0兼容的正则表达式,AWK-like正则表达式, glob表达式。还提供替换,分割,文件名过虑等功能package com.yesky.apachelog.util.regex;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;/** * 解析字符串: 返回符合记录的一行记录 ** @author 110 **/public class ApacheRegexString {/** * 返回符合条件的字符串 ** @param aLineString * @return:返回符合的字符,否则返回null */public static String regexString(String aLineString) {String regex = "\\s'GET\\s([^']+\\s)";PatternCompiler compiler = new Perl5Compiler();Pattern pattern = null;try {pattern = compiler.compile(regex);} catch (MalformedPatternException e) {// TODO Auto-generated catch blocke.printStackTrace();}PatternMatcher matcher = new Perl5Matcher();if (matcher.contains(aLineString, pattern)) {MatchResult result = matcher.getMatch();return result.group(1);}return null;}public static void main(String[] args) { ApacheRegexString reg=new ApacheRegexString();String s="124.135.38.249 - - 'GET /1.gif?&574224&613&1 HTTP/1.1' 200 35 'http://movie.yesky.com/movie613.html' 'Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1; SV1; QQPinyinSetup 620; TencentTraveler 4.0)'";String ss=reg.regexString(s);System.out.println(ss);} } 在这里附上对应的jar包
页:
[1]