Regex-Matching without creating unneeded array entry
04:09 07 Aug 2024

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 "$element\n"
        }
}

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:

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 /(?

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.

arrays regex perl