使用 reg 表达式读取日志文件时出错
我正在尝试读取内容如下的日志文件:
127.0.0.1 - - [17/OCT/2009:00:02:14 0000] GET xxxxxx xxxx xxx
我尝试了以下 reg exp 并收到 ERROR: Unknown group close index 90
regex = (\d+\.\ d+\.\d+\.\d+)\s-\s-\s\[(\d+)/(\w{3})/(\d{4}):(\d{2}):( \d{2}):(\d{2})\s(\d{4}\)].*
有人可以帮助我吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你忘记转义一些字符:
You forgot escaping some chars:
我认为“[”和“]”应该被转义:
[[]
和[]]
或\[
和\ ]
。对于Java:
I think the "[" and "]" should be escaped:
[[]
and[]]
or\[
and\]
.For Java:
首先,用反斜杠转义 [ 和 ]。它们在正则表达式中具有特殊含义。
First, escape [ and ] with backslahes. They have special meaning in regexps.
[ 和 ] 是特殊字符。这就是非封闭组的含义。根据您的正则表达式风格,您需要在每个括号前面放置 1 \ 或 2 \ 。
正则表达式 = (\d+.\d+.\d+.\d+)\s-\s-\s[(\d+)/(\w{3})/(\d{4}):(\d{2 }):(\d{2}):(\d{2})\s(\d{4})].*
[ 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})].*