in reply to Re: get n lines before or after a pattern
in thread get n lines before or after a pattern

Nice (and + +), but the regex can go astray:
C:>perl -E "my $word="jackhammer"; if ($word =~ /\bjack\b/) { say $word; } else { say \"No word-boundry-delimited 'jack's' found in $word\"; }" No word-boundry-delimited 'jack's' found in jackhammer

Replies are listed 'Best First'.
Re^3: get n lines before or after a pattern
by Kenosis (Priest) on Jul 25, 2012 at 21:32 UTC

    Perhaps I'm missing something, but I wouldn't want to find "jackhammer" if I were searching for "jack" as the first name--as listed in the OP's data set. However, the non-word-boundary regex is perfect for finding all first names containing the sub-string "jack", as $vals[2] =~ /jack/ would.

      Flip that coin. It's not a question of searching for "jackhammer." It's a matter of what *jack* elements could be present in the data (whose contents are, at least possibly, unknown at the time you start writing the code)... as, for example, jackson (mississippi) or jackson hole, colorado.

      But note: my reply is directed, not to you, but to the parent of your node... and, thus, casts no aspersions on your prior remark; in fact, it appears to say pretty much the same thing, but you said it first

        I do understand your point. However, the OP appears to be only interested in an exact first name match, given the data set and specific example output. Additionally, the OP says, "...I want to search for a particular first name..." (emphasis mine); sub-string matching does not return a particular first name. Based on these factors, it made sense to force an exact first name match--as the OP's desired output showed. It's certainly easy enough, however, to remove the regex's \bs if my exact-match assumption is incorrect.

        Thanks for addressing this issue...