tags:

views:

129

answers:

5

Consider a log file which contains

r100000|Tom Sawyer|2010-12-01|view.txt

I should parse this and print

ID:r100000
NAME:Tom Sawyer
DATE:2010-12-01
FILENAME:view.txt

I should only use regular expressions.

A: 

You don't mention the dialect of RE. But for instance:

$ echo 'r100000|Tom Sawyer|2010-12-01|view.txt' | \
  perl -pe 's/^(r\d+)\|([^|]+)\|([0-9-]+)\|(.+)/ID:\1 NAME:\2 DATE:\3 FILENAME:\4/'
ID:r100000 NAME:Tom Sawyer DATE:2010-12-01 FILENAME:view.txt
Phil P
+3  A: 

the easier way is to break your string into fields, using delimiters. Since you have pipe "|" as delimiters, then use it. No need for complicated regex. Plus, what if you have more fields next time?.

Here's one with awk (you can use -F option of Perl as well)

$ awk -F"|" '{print "ID:"$1" Name:"$2" Date:"$3" filename:"$4}' file
ID:r100000 Name:Tom Sawyer Date:2010-12-01 filename:view.txt

Perl equivalent

$ perl -F"\|" -ane 'print "ID:$F[1] Name: $F[2] Date:$F[3] filename:$F[4]"' file
ID:Tom Sawyer Name: 2010-12-01 Date:view.txt
ghostdog74
+3  A: 
$line = 'r100000|Tom Sawyer|2010-12-01|view.txt';
@fields = split /\|/, $line;
print $fields[0]; # r100000
knittl
+1  A: 

If you want to use Regular expression to parse it,

you can try this one:

$line = r100000|Tom Sawyer|2010-12-01|view.txt;

if($line =~ /^([^|]+)\|([^|]+)\|([^|]+)\|([^|]+)$/)
{
$id = $1;
$name = $2;
$date = $3;
$filename = $4
}
Nikhil Jain
+1  A: 
use Data::Dumper;
my %h;
my $line = 'r100000|Tom Sawyer|2010-12-01|view.txt';
@h{qw/ID NAME DATE FILENAME/} = (split /\|/, $line);
print Dumper(\%h);
M42