使用正则表达式从日志文件中提取信息

时间:2013-01-12 20:21:00

标签: regex

处理以下问题:http://regexone.com/example/6

我发现自己无法使用正则表达式捕获第一个括号。到目前为止,这是我的正则表达式:at (\w+).(\w+)\.(\w+)

这是我的正则表达式应处理的示例行:at widget.List.makeView(ListView.java:1727)

5 个答案:

答案 0 :(得分:1)

要捕获某些括号之间的所有内容,请包含括号:

(\(.*?\))。例如,这将在捕获组1中放置'(ListView.java:1727)',根据正则表达式的风格,您可以引用\1

因此,(\(.*?\))最终会以'(ListView.java:1727)'通过\ 1进行访问。

如果要在括号内匹配,但不在捕获中捕获括号本身,则可以执行:\((.*?)\)。现在\ 1将是'ListView.java:1727'。

如果您想在括号内获取个别内容,可以执行\((.*?):(.*?)\)之类的操作。这将使\ 1成为'ListView.java',\ 2成为'1727'。

这有帮助吗?

答案 1 :(得分:0)

如果我只是给你一个正常的正则表达式,不确定你是否真的会学到任何东西,但是你走了:

at [^\.]+\.[^\.]+\.([^\.]+)\((.+):(\d+)\)

或者更简单一点:

at \w+\.\w+\.(\w+)\((\w+\.\w+):(\d+)\)

答案 2 :(得分:0)

我用过

.*\..*\.(\w+)\((\w+\.\w+):(\d+)\)

答案 3 :(得分:0)

我会使它更通用,即

/at ([\w.]+)\(([^:]+):(\d+))

内存捕获是:

  1. 班级
  2. 文件名
  3. 行号

答案 4 :(得分:0)

我参加派对有点晚了,但是使用这种模式可以让你点击继续:

([\w]+).([\w]+\.[\w]+):([\d]+)

(        //First group
[\w]     //Match a single character
+        //Between 1 and x times
)        //End of first group
.        //any character (\W works too, \( unfortunately not)
(        //2nd Capturing group ([\w]+\.[\w]+)
[\w]     //Match a single character
+        //Between 1 and x times
\.       //The literal . char
[\w]     //Match a single character
+        //Between 1 and x times
)        //end of 2nd group
:        // Match colon char
(        //Start of 3rd group
[\d]     //Match any digits
+        // one or more times
)        //End of third group

可能有更清晰的选择,但这是regexone.com接受的一种方式