Meenakshi
Meenakshi

Reputation: 11

Matched elements using regex in Perl

I have a hash which contains a regular expression: the number of matches to be captured in it and variables and their position of match. For example:

my %hash = (
    reg_ex => 'Variable1:\s+(.*?)\s+\n\s+Variable2:\s+(.*?)\s+\n',
    count => 2,
    Variable1 => 1,
    Variable2  => 2,
);

I am going to use this regex in some other part of code where I will be just giving say $to_be_matched_variable =~ /$hash{reg_ex}/ and we obtain the required matches here in $1, $2, ...

I need to use the value of the key Variable1, which indicates the number of the match to be used in place where we normally use $1.

I tried giving $.$hash{Variable1} and $,$hash{Variable1}. I am not able to find how to frame something that will be equivalent to $1, $2...

Upvotes: 1

Views: 313

Answers (3)

dreamlax
dreamlax

Reputation: 95335

Try:

(my @ArrayOfMatches) = $to_be_matched_variable =~ /$hash{reg_ex}/;

my $Variable1 = $ArrayOfMatches[$hash{Variable1}];

Upvotes: 7

innaM
innaM

Reputation: 47829

Since you are already using a hash, you might as well use the builtin %+ which maps names to matches. Thus if you changed your regexes to named matching, you could easily use %+ to retrieve the matched parts.

$reg_ex = 'Variable1:\s+(?<foo>.*?)\s+\n\s+Variable2:\s+(?<bar>.*?)\s+\n';

After a successful match, %+ should have the keys foo and bar and the values will correspond to what was matched.

Thus your original hash could be changed to something like this:

my %hash = (
    reg_ex => 'Variable1:\s+(?<foo>.*?)\s+\n\s+Variable2:\s+(?<bar>.*?)\s+\n',
    groups => [ 'foo', 'bar' ],
);

Upvotes: 4

Ingo
Ingo

Reputation: 36339

($1, $2, $3, ...., $9)[$hash{Variable1}]

Upvotes: 3

Related Questions