简体   繁体   中英

Extracting information from a log file using a regex

Working on the following problem: http://regexone.com/example/6 ?

I'm finding myself not able to capture the first parenthesis using of a regex. This is my regex so far: at (\\w+).(\\w+)\\.(\\w+)

This is a sample line my regex should process: at widget.List.makeView(ListView.java:1727)

To capture everything between some parentheses, include the parenthesis:

(\\(.*?\\)) . This would place, for example, '(ListView.java:1727)' in capture group 1, which you could, depending on regex flavor, reference as \\1 .

So, (\\(.*?\\)) will end up with '(ListView.java:1727)' accessible via \\1.

If you want to match inside the parentheses, but not capture the parentheses themselves as part of the capture, you could do: \\((.*?)\\) . Now \\1 would be 'ListView.java:1727'.

If you want to get individual things within the parentheses, you could do something like \\((.*?):(.*?)\\) . That will make \\1 be 'ListView.java' and \\2 be '1727'.

Does that help?

Not sure if you actually will learn anything, if I just give you a working regular expression, but here you go:

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

Or a little bit simpler:

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

我用了

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

I would make it a bit more generic, ie

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

The memory captures are:

  1. The class
  2. The file name
  3. The line number

I'm a little late to the party, but using this pattern lets you click continue:

([\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

There may be cleaner options, but this is one way regexone.com accepts as an answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM