poncenby wrote:
> i have a file which has lines of text with fields separated by a space.
> some of the fields are prefixed with a number and a space, like the
> line below...
>
> bar1 bar2 XX 10 bar3tooten
> foo1 foo2 XX 15 foo3uptofifteen
>
> as you can see, the numbers (10 and 15) are the length of the field
> after the number.
> so i want to use these numbers as length specifier to match the field
> after the number, with a regex like either of these:
>
> /(.+)\s(.+)\sXX\s([0-9)+)\s(.{$3})/
]-----------------------^
> /(.+)\s(.+)\sXX\s([0-9)+)\s(.{\3})/
]-----------------------^
>
> both regexs will make the program fall over when attempting to print
> $4.
>
> i've figured out a solution with a regex over two lines but am curious
> why this doesn't work.
Doesn't work because of the syntax error. And because the contents of
the {...} construction have to be literal digits or digits,digits .
For a one-liner, try something like:
use strict;
use warnings;
my $v;
while(<DATA>){
chomp;
s/^(.+)\s(.+)\sXX\s(\d+)\s(.*)/$v=substr($4,0,$3);"$1 $2 XX $3 $4";/e;
print "line:$_:\nv:$v:\n";
}
__END__
bar1 bar2 XX 10 bar3tootenblahblahblah
foo1 foo2 XX 15 foo3uptofifteenyadayadayada
(Data was padded to illustrate that it works.) The second expression in
the replacement expression is present so the value of the replacement
string is the same as the original string so the "matched" variable is
preserved in the substitution. Also, I anchored the start so it won't
match starting partway through a line. Generates:
D:\junk>perl junk574.pl
line:bar1 bar2 XX 10 bar3tootenblahblahblah:
v:bar3tooten:
line:foo1 foo2 XX 15 foo3uptofifteenyadayadayada:
v:foo3uptofifteen:
D:\junk>
....
> poncenby
--
Bob Walton
Email:
http://bwalton.com/cgi-bin/emailbob.pl