Reputation: 449
I am trying to save matched patterns in an array using perl and regex, the problem is that when the match is saved it is missing some characters
ex:
my @array;
my @temp_array;
@types_U8 = ("uint8","vuint8","UCHAR");
foreach my $type (@types_U8)
{
@temp_array = $str =~ /\(\s*\Q$type\E\s*\)\s*(0x[0-9ABCDEF]{3,}|\-[1-9]+)/g;
push(@array,@temp_array);
@temp_array = ();
}
So if $str = "any text (uint8)-1"
The saved string in the @temp_array
is only ever "-1"
Upvotes: 2
Views: 164
Reputation: 61520
Your current regular expression is:
/\(\s*\Q$type\E\s*\)\s*(0x[0-9ABCDEF]{3,}|\-[1-9]+)/g
this means
\(
\s*
$type
: \Q$type\E
\s*
\)
\s*
(
0x[0-9ABCDEF]{3,}|\-[1-9]+
)
If you notice above, your capturing group doesn't start until step #7, when you would also like to capture $type
and the literal parens.
Extend your capturing group to enclose those areas:
/(\(\s*\Q$type\E\s*\)\s*(?:0x[0-9ABCDEF]{3,}|\-[1-9]+))/;
This means:
(
\(
\s*
$type
: \Q$type\E
\s*
\)
\s*
(?:
0x[0-9ABCDEF]{3,}|\-[1-9]+
)
)
(Note: I removed the g
(global) modifier because it is unnecessary)
This change gives me a result of (uint8)-1
Upvotes: 2