带有 php 的正则表达式 websphere 文件

发布于 2024-12-06 20:05:09 字数 555 浏览 2 评论 0原文

我正在尝试为这种日志创建正则表达式:

[9/23/11 11:09:53:632 CEST] 0000005b FormLoginExte 3 基于表单的登录:表单中存在用户 ID/密码。用户是:user1

我创建了这个:

preg_match("/^[\d+/\d+/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8}\ \w+?\ +\w+?\ +. +?$", $line, $matches); // 格式化行的模式

并收到此错误:

[9/21/11 12:11:03:126 CEST] 0000004d FormLoginExte 3 基于表单的登录:表单中存在用户 ID/密码。用户是:user1
警告:preg_match() [function.preg-match]:第 79 行 C:\AppServ\www\logWebsphere\logWebspere.php 中的未知修饰符 '\'

i'm trying to create a regular expression for this kind of log:

[9/23/11 11:09:53:632 CEST] 0000005b FormLoginExte 3 Form based login: userid/password present in the form. User is: user1

i've created this:

preg_match("/^[\d+/\d+/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8}\ \w+?\ +\w+?\ +.+?$", $line, $matches); // pattern to format the line

and get this error:

[9/21/11 12:11:03:126 CEST] 0000004d FormLoginExte 3 Form based login: userid/password present in the form. User is: user1
Warning: preg_match() [function.preg-match]: Unknown modifier '\' in C:\AppServ\www\logWebsphere\logWebspere.php on line 79

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

回忆追雨的时光 2024-12-13 20:05:09

正则表达式 (preg_match) 比运行简单的 POC 慢

<?php
$data = explode(" ","[9/23/11 11:09:53:632 CEST] 0000005b FormLoginExte 3 Form based login: userid/password present in the form. User is: user1");

//data now contains all the info that you need
var_dump($data);
?>

http://codepad.org/nmo90QlI

Regular expressions (preg_match) are slower than running a simple:

<?php
$data = explode(" ","[9/23/11 11:09:53:632 CEST] 0000005b FormLoginExte 3 Form based login: userid/password present in the form. User is: user1");

//data now contains all the info that you need
var_dump($data);
?>

POC here: http://codepad.org/nmo90QlI

热血少△年 2024-12-13 20:05:09

您使用正斜杠作为分隔符,但也在正则表达式中包含未转义的正斜杠。您应该首先在它们前面加上两个反斜杠来转义它们。需要两个反斜杠,因为反斜杠本身就是元字符,必须使用第二个反斜杠进行转义。这实际上创建了一个反斜杠,可用于转义分隔符。

所以你的正则表达式变成:

preg_match("/^[\d+\\/\d+\\/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8}\ \w+ ?\ +\w+?\ +.+?$/", $line, $matches);

请注意,您还缺少分隔符。

此外,您可以简单地更改分隔符,如下所示:

preg_match("~^[\d+\/\d+\/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8 }\ \w+?\ +\w+?\ +.+?$~", $line, $matches);

编辑

我刚刚注意到你正在尝试匹配[] 作为文字。然而,这些是元字符(字符类),我相信也应该被转义( \[\] )。

You are using forward slashes as your delimeter but are also including unescaped forward slashes in your regex. You should escape them first by preceding them with a two backslashes. Two backslashes are required because the backslash is itself a meta character and must be escaped by using a second backslash. This in effect creates one backslash which can be used to escape the delimeter.

So your regex becomes:

preg_match("/^[\d+\\/\d+\\/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8}\ \w+?\ +\w+?\ +.+?$/", $line, $matches);

Please note you were also missing a delimiter.

Additionally, you could simply change delimters, like so:

preg_match("~^[\d+\/\d+\/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8}\ \w+?\ +\w+?\ +.+?$~", $line, $matches);

Edit

I've just noticed that you are trying to match [ and ] as literals. These, however, are meta characters (character classes) and should, I believe, also be escaped( \[ and \]).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文