iwahdan
iwahdan

Reputation: 449

cant save pattern matches in array using perl and regex

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

Answers (1)

Hunter McMillen
Hunter McMillen

Reputation: 61520

Your current regular expression is:

/\(\s*\Q$type\E\s*\)\s*(0x[0-9ABCDEF]{3,}|\-[1-9]+)/g

this means

  1. match a literal left paren: \(
  2. match zero or more whitespace characters: \s*
  3. match the value that is stored in $type: \Q$type\E
  4. match zero of more whitespace characters: \s*
  5. match a literal right paren: \)
  6. match zero of more whitespace characters: \s*
  7. START capturing group: (
  8. match a 3 digit hexadecimal number prefixed with 0x OR match a literal dash, followed by 1 or more digits from 1 to 9: 0x[0-9ABCDEF]{3,}|\-[1-9]+
  9. END capturing group: )

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:

  1. START a capturing group: (
  2. match a literal left paren: \(
  3. match zero or more whitespace characters: \s*
  4. match the value that is stored in $type: \Q$type\E
  5. match zero of more whitespace characters: \s*
  6. match a literal right paren: \)
  7. match zero of more whitespace characters: \s*
  8. START non-capturing group: (?:
  9. match a 3 digit hexadecimal number prefixed with 0x OR match a literal dash, followed by 1 or more digits from 1 to 9: 0x[0-9ABCDEF]{3,}|\-[1-9]+
  10. END non-capturing group: )
  11. END capturing group: )

(Note: I removed the g (global) modifier because it is unnecessary)

This change gives me a result of (uint8)-1

Upvotes: 2

Related Questions