Jump to content

regexp


widowmaker

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/204950-regexp/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/204950-regexp/#findComment-1073043
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/204950-regexp/#findComment-1073178
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/204950-regexp/#findComment-1073315
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.