开发者

regex websphere file with php

开发者 https://www.devze.com 2023-04-07 09:58 出处:网络
i\'m trying to create a regular expression for this kind of log: [9/23/11 11:09:53:632 CEST] 0000005b FormLoginExte 3Form based login: userid/password present in the form. User is: user1

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

开发者_开发技巧


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


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 \]).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号