BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #23326同步于 2012/8/30
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

[问题]求正则表达式大牛帮忙写一个正则表达式呀

czct
2012/8/30镜像同步5 回复
求大牛帮忙哈, 118.195.65.153 - - [01/Aug/2012:00:00:02 +0800] "POST /cart/cart.jsp HTTP/1.1" 200 17865 "http://shopping.xx.com/cart/cart.jsp?curent_time=&productId=128893&quantity=1&process=add&fromSite=tuan&skuId=130953&" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; 360SE)" "-" 0.213 上面为一行访问日志,一共11项,想将它的每一项都分解出来,想用正则表达式来分解,哪位大牛能帮下忙呀,拜谢。
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
wks机器人#1 · 2012/8/30
试试这个。Scala语言,但是这个正则Java也能用,可以拷贝到Java里。 package com.github.wks.junk.scala object ApacheLogRegexTest extends App { val regex = "([^ \\[\\]\"]+)|(\\[[^\\]]+?\\])|(\"[^\"]+?\")".r val target = """118.195.65.153 - - [01/Aug/2012:00:00:02 +0800] "POST /cart/cart.jsp HTTP/1.1" 200 17865 "http://shopping.xx.com/cart/cart.jsp?curent_time=&productId=128893&quantity=1&process=add&fromSite=tuan&skuId=130953&" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; 360SE)" "-" 0.213""" val all = regex.findAllIn(target).toList all.foreach { s => println(s) } println("size = %d".format(all.size)) } 输出 118.195.65.153 - - [01/Aug/2012:00:00:02 +0800] "POST /cart/cart.jsp HTTP/1.1" 200 17865 "http://shopping.xx.com/cart/cart.jsp?curent_time=&productId=128893&quantity=1&process=add&fromSite=tuan&skuId=130953&" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; 360SE)" "-" 0.213 size = 11 【 在 czct 的大作中提到: 】 : 求大牛帮忙哈, : 118.195.65.153 - - [01/Aug/2012:00:00:02 +0800] "POST /cart/cart.jsp HTTP/1.1" 200 17865 "http://shopping.xx.com/cart/cart.jsp?curent_time=&productId=128893&quantity=1&process=add&fromSite=tuan&skuId=130953&" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; 360SE)" "-" 0.213 : 上面为一行访问日志,一共11项,想将它的每一项都分解出来,想用正则表达式来分解,哪位大牛能帮下忙呀,拜谢。
czct机器人#2 · 2012/8/31
【 在 wks 的大作中提到: 】 : 试试这个。Scala语言,但是这个正则Java也能用,可以拷贝到Java里。 : [code=scala] : package com.github.wks.junk.scala : ................... val regex = "([^ \\[\\]\"]+)|(\\[[^\\]]+?\\])|(\"[^\"]+?\")".r 这句话的.r是什么意思。。
wks机器人#3 · 2012/8/31
这是Scala语言。.r是把字符串转换成Regex对象。 Java不是这样。Java要 import java.util.regex.*; Pattern regex = Pattern.compile("把那个正则表达式贴到这里") Matcher m = regex.matcher(target); .... 【 在 czct 的大作中提到: 】 : val regex = "([^ \\[\\]\"]+)|(\\[[^\\]]+?\\])|(\"[^\"]+?\")".r 这句话的.r是什么意思。。
wks机器人#4 · 2012/8/31
注意是不断地find()直到把11项都取出来。 package com.github.wks.javajunks; import java.util.regex.*; public class RegexTest2 { public static void main(String[] args) { Pattern regex = Pattern.compile("([^ \\[\\]\"]+)|(\\[[^\\]]+?\\])|(\"[^\"]+?\")"); String target = "118.195.65.153 - - [01/Aug/2012:00:00:02 +0800] \"POST /cart/cart.jsp HTTP/1.1\" 200 17865 \"http://shopping.xx.com/cart/cart.jsp?curent_time=&productId=128893&quantity=1&process=add&fromSite=tuan&skuId=130953&\" \"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; 360SE)\" \"-\" 0.213"; Matcher m = regex.matcher(target); int numMatches = 0; while(m.find()) { System.out.println(m.group()); numMatches += 1; } System.out.format("numMatches = %d\n", numMatches); } } 118.195.65.153 - - [01/Aug/2012:00:00:02 +0800] "POST /cart/cart.jsp HTTP/1.1" 200 17865 "http://shopping.xx.com/cart/cart.jsp?curent_time=&productId=128893&quantity=1&process=add&fromSite=tuan&skuId=130953&" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; 360SE)" "-" 0.213 numMatches = 11 【 在 czct 的大作中提到: 】 : val regex = "([^ \\[\\]\"]+)|(\\[[^\\]]+?\\])|(\"[^\"]+?\")".r 这句话的.r是什么意思。。
zx723机器人#5 · 2012/9/6
学习了,话说我邮的论坛支持code高亮啊,才知道。test一下,不知道标签是什么。 [code]public class Test[/code]