tags:

views:

62

answers:

5

I am trying to read a log file with the content look like this:

127.0.0.1 -  - [17/OCT/2009:00:02:14 0000] GET xxxxxx  xxxx  xxx 

I tried the following reg exp and I am getting ERROR: Unclosed group near index 90

regex = (\d+\.\d+\.\d+\.\d+)\s-\s-\s\[(\d+)/(\w{3})/(\d{4}):(\d{2}):(\d{2}):(\d{2})\s(\d{4}\)].*

Can someone help me?

+1  A: 

I think the "[" and "]" should be escaped: [[] and []] or \[ and \].

For Java:

java.util.regex.Pattern.compile("(\\d+.\\d+.\\d+.\\d+)\\s-\\s-\\s\\[(\\d+)/(\\w{3})/(\\d{4}):(\\d{2}):(\\d{2}):(\\d{2})\\s(\\d{4})\\].*")
Thomas Jung
+2  A: 

You forgot escaping some chars:

^(\d+\.\d+\.\d+\.\d+)\s-\s-\s\[(\d+)\/(\w{3})\/(\d{4}):(\d{2}):(\d{2}):(\d{2})\s(\d{4})\]
Patonza
A: 

First, escape [ and ] with backslahes. They have special meaning in regexps.

Dmitry
A: 

[ and ] are special characters. That's what it means by unclosed group. Depending on your flavor of regex, you'll need to put either 1 \ or 2 \ in front of each bracket.

regex = (\d+.\d+.\d+.\d+)\s-\s-\s[(\d+)/(\w{3})/(\d{4}):(\d{2}):(\d{2}):(\d{2})\s(\d{4})].*

MWill
1 or 2 backslashes doesn't depend on the regex flavor but rather on whether the language you're using requires backslashes to be escaped.
Jeremy Stein
A: 
^\d+\.\d+\.\d+\.\d+\s-\s-\s\[\d{2}\/[A-Z]{3}\/\d{4}:\d{2}:\d{2}:\d{2}\s\d{4}]\sGET\s(.{6}\s.{4}\s.{3})$
Jordan Messina