widowmaker Posted June 16, 2010 Share Posted June 16, 2010 hi, I'm trying to parse a log file, but I found out I've got a bit mess in my regexp knowledge so I might need your help. Let's say I've got the following row from a log file: 2009/06/28 22:54:59 administrator 13 1 the same: 2009/06/28(space)22:54:59(space)administrator(tab)13(tab)1 and I'd like to rip out get this info: $reg[0]=2009 $reg[1]=06 $reg[2]=28 $reg[3]=22 $reg[4]=54 $reg[5]=59 $reg[6]=administrator $reg[7]=13 $reg[8]=1 I think regexp should be able to do this work but Idk how to script the code. Thanx for any help! Quote Link to comment https://forums.phpfreaks.com/topic/204950-regexp/ Share on other sites More sharing options...
damaya Posted June 16, 2010 Share Posted June 16, 2010 hi, I'm trying to parse a log file, but I found out I've got a bit mess in my regexp knowledge so I might need your help. Let's say I've got the following row from a log file: 2009/06/28 22:54:59 administrator 13 1 the same: 2009/06/28(space)22:54:59(space)administrator(tab)13(tab)1 and I'd like to rip out get this info: $reg[0]=2009 $reg[1]=06 $reg[2]=28 $reg[3]=22 $reg[4]=54 $reg[5]=59 $reg[6]=administrator $reg[7]=13 $reg[8]=1 I think regexp should be able to do this work but Idk how to script the code. Thanx for any help! You should be able to match like this: 2009/06/28 22:54:59 administrator 13 1 /^(\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\d{2})\d{2})\s(\w+?)\t(\d+?)\t(\d+)$/ A quick Perl script test: #!/usr/bin/perl use strict; use warnings; my $test="2010/06/28 22:54:59 administrator 13 1"; my @test = $test =~ /^(\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\d{2})\d{2})\s(\w+?)\t(\d+?)\t(\d+)$/g; foreach my $result (@test) { print "$result\n"; } Resulted in this output: 2010 06 28 22 54 59 administrator 13 1 Quote Link to comment https://forums.phpfreaks.com/topic/204950-regexp/#findComment-1073043 Share on other sites More sharing options...
widowmaker Posted June 16, 2010 Author Share Posted June 16, 2010 Ok, I've tried this in PHP and I've got this error: Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'g' actually, I've been wondering what those modifiers actually do. I mean I've seen some regex ending with /e, some with /i, what does it mean? anyway thanx Quote Link to comment https://forums.phpfreaks.com/topic/204950-regexp/#findComment-1073178 Share on other sites More sharing options...
salathe Posted June 17, 2010 Share Posted June 17, 2010 Instead of using regex, you could use a function which is designed to parse formatted strings: sscanf $subject = '2009/06/28 22:54:59 administrator 13 1'; $reg = sscanf($subject, '%d/%d/%d %d:%d:%d %s %d %d'); The format string could be more, or less, precise depending on your needs (e.g. if you need the leading zero for the date/time values less than 10). Quote Link to comment https://forums.phpfreaks.com/topic/204950-regexp/#findComment-1073315 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.