A negative pattern matching creates an unused array element.
Given is a file containing lines like these:
FILE:abc LENGTH:123 AUTHOR:Bobby
FILE:xyz LENGTH:987 AUTHOR:Sabine
I need to splice this line by line into the columns but ignore the FILE column
while ($line=<>) {
print "$line";
foreach $element ( $line=~/(S+)/g ) {
print "$elementn"
}
}
running does output:
FILE:abc LENGTH:123 AUTHOR:Bobby
FILE:abc
LENGTH:123
AUTHOR:Bobby
FILE:xyz LENGTH:987 AUTHOR:Sabine
FILE:xyz
LENGTH:987
AUTHOR:Sabine
But I need:
running does output:
FILE:abc LENGTH:123 AUTHOR:Bobby
LENGTH:123
AUTHOR:Bobby
FILE:xyz LENGTH:987 AUTHOR:Sabine
LENGTH:987
AUTHOR:Sabine
I already know about negative look ahead/behind and in theory it works well, it looks something like /(?<!FILE:)(S+)/g but for my case this isn’t good enough as it has one big drawback:
It creates another array element for the foreach loop which changes the output to:
FILE:abc LENGTH:123 AUTHOR:Bobby
LENGTH:123
AUTHOR:Bobby
FILE:xyz LENGTH:987 AUTHOR:Sabine
LENGTH:987
AUTHOR:Sabine
As the position of the columns isn’t stable I can not just use an array range.
So, is it even possible to create a regex with ignores the FILE column but does not create an additional array element?
Background: I am giving you an extremely simplified version of my loop, please bear with me if my problem seem trivial. Sure, I can use an additional pattern-matching to filter out the unwanted column but it would turn a lot of things upside down later in the code. Also, I consider this an interesting programming exercise.