Regexp for variable length tags

J

Jon Burroughs

I am processing some data that has a up to three key-value pairs
concatenated together. The keys can be "ADD, REM, EQD". Values are
variable length.

There will always be an "ADD" section, followed by 0 to 1 "REM"
sections, followed by 0 to 1 "EQD" sections. For example:
ADDxxxxxxxxREMyyyyyEQDzzzzz

I'm trying to find a regular expression that will split this apart into
separarate sections in one step.

So far, I have this:

$rec =~ /(ADD.+)(REM.+)(EQD.+)/;

But, this only works if I know the record has all three tokens.

This gobbles too much:
$rec =~ /(ADD.+)(REM.+)?(EQD.+)?/;

Any ideas?

-Jon
 
J

John W. Krahn

Jon said:
I am processing some data that has a up to three key-value pairs
concatenated together. The keys can be "ADD, REM, EQD". Values are
variable length.

There will always be an "ADD" section, followed by 0 to 1 "REM"
sections, followed by 0 to 1 "EQD" sections. For example:
ADDxxxxxxxxREMyyyyyEQDzzzzz

I'm trying to find a regular expression that will split this apart into
separarate sections in one step.

So far, I have this:

$rec =~ /(ADD.+)(REM.+)(EQD.+)/;

But, this only works if I know the record has all three tokens.

This gobbles too much:
$rec =~ /(ADD.+)(REM.+)?(EQD.+)?/;

Any ideas?

Try using non-greedy quantifiers.

perldoc perlre


John
 
G

Gunnar Hjalmarsson

Jon said:
There will always be an "ADD" section, followed by 0 to 1 "REM"
sections, followed by 0 to 1 "EQD" sections. For example:
ADDxxxxxxxxREMyyyyyEQDzzzzz

I'm trying to find a regular expression that will split this apart into
separarate sections in one step.

Why regex?

my @rec;
while (<DATA>) {
chomp;
for my $key ( qw/EQD REM ADD/ ) {
if( (my $pos = index $_, $key) >= 0 ) {
$rec[$.-1]{$key} = substr $_, $pos+3;
substr $_, $pos, 100, '';
}
}
}
use Data::Dumper;
print Dumper \@rec;

__DATA__
ADDxxxxxxREMyyyyyEQDzzzzz
ADD2222REM666666
ADD7777777EQD8888
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top