user3521305
user3521305

Reputation: 411

Perl Grep command to search in array

I am using Perl and grep command to look for pattern in an array output. I am interested in searching for the following text in an array:

/tmp/12345.hash

The 12345 could be any sequence of numbers like 234 or 567889 but the /tmp/ and the .hash will be consistent. I am not great with regex therefore not sure how to build the proper regex statement.

@line = grep /hash/, @exp;

My original search was to look just the word hash but that matched on other lines and i ended up with the wrong result.

Upvotes: 0

Views: 577

Answers (1)

zdim
zdim

Reputation: 66873

Regex allows you to encode requirements into a "pattern" with a lot more precision:

my @filtered = grep { m{^/tmp/[0-9]+\.hash$} } @all;

I use {} for delimiters since with the usual // ones every / in the pattern must be escaped. Then m is required in front (unlike with // delimiters where it may be omitted).

The anchor ^ matches the beginning of the string (and at yet other positions if the /m "modifier" is in effect). The /tmp seems to be the beginning of a path, but if there were (for example) leading spaces before it then the above wouldn't match (unless you change it to ^\s*/tmp so to allow for optional spaces). Consider your data carefully.

The $ matches the end of the string, or before the newline at the end if there is one (/m modifier changes this). To also match strings with more characters after hash remove the $.

The pattern itself sets down what you say in the problem description: there must be an integer, which varies, and the rest is fixed.

Perl's own (excellent) documentation comes with the tutorial perlretut.


  With the modifier, $str =~ /.../m, the string is treated as a multi-line string so that if there are linefeeds in it then ^ and $ in that regex match the beginning and end of each line as well.

The anchor to always match only the end of the string is \z (also see \Z which matches like $ but is insensitive to /m). See Assertions in perlre, and see answers on this page.

Upvotes: 4

Related Questions