Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get perl grep do perform regex expression capturing

Tags:

perl

I'm trying to capture a substring using regex capturing with grep by getting the content of (.*) in the code below.

            @substring = grep /^test-results(.*)/,@$(array_reference);

This is not working....

like image 547
Yetimwork Beyene Avatar asked May 29 '12 00:05

Yetimwork Beyene


People also ask

How do I match a regular expression in Perl?

Perl Matching Operator =~The matching operator =~ is used to match a word in the given string. It is case sensitive, means if string has a lowercase letter and you are searching for an uppercase letter then it will not match.

What is * in Perl regex?

Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“.

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".


2 Answers

In list context, a regex match returns a list of what its captures matched, so all you need is:

@substrings = map /^test-results(.*)/, @$array;
like image 151
ikegami Avatar answered Oct 13 '22 18:10

ikegami


Probably the map function is a better fit for what you want. You're looking for something similar to the following (untested) code:

@substrings = map { /^test-results(.*)/ ? $1 : () } @{ $arrayref };
like image 29
sarnold Avatar answered Oct 13 '22 18:10

sarnold