PerlGrepExactMatch

Want to perform an exact match with Perl's grep function?

Perl's grep function is incredibly powerful. However, at first glance it can seem as if Regular Expressions are going to trip you up. For example, if you want to perform an exact match with data that might contain Regualr Expression special characters. For example, this looks like it might be a problem...

my @elements=("apple", "apple.", "apple1", "apple2", "banana");

 my @results=grep(/apple/,@elements);5

Here, @results will contain "apple", "apple.", "apple1" and "apple2". This can or course be avoided as follows:

my @results=grep(/^apple$/,@elements);

Which will only put "apple" into the results. But what if the search term contains metacharacters (like . * ? etc)? For this you need a non-regular expression version of grep. Here's an example:

my @results=grep( { "$_&" eq "apple." } @elements);

Here, @results will only contain "apple.", because it's the only thing that matches exactly. The check is a simple string "eq" rather than a regular expression, so metacharacters aren't used. Rememeber, this won't work:

my @results=grep(/^apple.$/, @elements);

...becuase the "." will match any single character. You'll end up with "apple.", "apple1" and "apple2" in @results.

ALSO SEE: PerlTextSort

Comments

match metacharacter

if you are interested only in "apple."
What about the following...

my @results=grep(/^apple\.$/, @elements);

seems simpler than
my @results=grep( { "$_" eq "apple." } @elements);

Re: match metacharacter

Yep, for sure. Indeed, you can use the quotemeta() function to do just that. However, in more complex scenarios, an exact match can be useful.

how about negating the match?

how do i get all occurrences that do not match "john"? i know on the command line you can do grep -v but how about perl?
Roumen Semov

Re: how about negating the match?

Here you go:
grep !/john/, @array1;
Roumen Semov

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.

More information about formatting options