使用 reg 表达式读取日志文件时出错

发布于 2024-08-11 20:08:38 字数 317 浏览 4 评论 0原文

我正在尝试读取内容如下的日志文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心如荒岛 2024-08-18 20:08:38

你忘记转义一些字符:

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

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})\]
夏至、离别 2024-08-18 20:08:38

我认为“[”和“]”应该被转义:[[][]]\[\ ]

对于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})\\].*")

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})\\].*")
夏九 2024-08-18 20:08:38

首先,用反斜杠转义 [ 和 ]。它们在正则表达式中具有特殊含义。

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

成熟稳重的好男人 2024-08-18 20:08:38

[ 和 ] 是特殊字符。这就是非封闭组的含义。根据您的正则表达式风格,您需要在每个括号前面放置 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})].*

森林迷了鹿 2024-08-18 20:08:38
^\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})$
^\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})$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文