带有 php 的正则表达式 websphere 文件
我正在尝试为这种日志创建正则表达式:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正则表达式 (preg_match) 比运行简单的 POC 慢
:http://codepad.org/nmo90QlI
Regular expressions (preg_match) are slower than running a simple:
POC here: http://codepad.org/nmo90QlI
您使用正斜杠作为分隔符,但也在正则表达式中包含未转义的正斜杠。您应该首先在它们前面加上两个反斜杠来转义它们。需要两个反斜杠,因为反斜杠本身就是元字符,必须使用第二个反斜杠进行转义。这实际上创建了一个反斜杠,可用于转义分隔符。
所以你的正则表达式变成:
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\]
).