http://qs321.pair.com?node_id=1204443


in reply to Re: question about finding strings (regexes and slurping files)
in thread question about finding strings?

First, EXCELLENT POINTS in Discipulus' reply above.

Refresher: OP wants to capture a specific word and some words around it (no definition of why) in any lines of a moderately large text file which contain the specific word -- for some sort of corpus analysis.

Now, for another approach to the regex, we can define $word (to capture the 'nearby words' OP wanted) in terms of alpha-content rather than by character-counting. (As done here, we must insert single spaces between the words in the regex itself but could have including most of them in the definition of $word):

#!/usr/bin/perl use strict; use warnings; use 5.024; # 1204380 my $string = 'tryna'; my $word = qr /[a-z]+/i; # any word comprised solely of letters a-z, UC or LC, follo +wed by a space my (@slurp, $line, @found, $found); # declare vars; bad practice to do as globals, but simpler +to read @slurp = <DATA>; # read each line of __DATA__ into var $slurp; for $line(@slurp) { # read thru array @slurp line by line if ( $line =~ /($word\s$word\s$string\s$word\s$word\s)/gix ) { # Match only if there are two $word instances before $s +tring and a # space following the second $word after $string push @found, "\tmatch: $1"; # When Ln 16 matches Ln 17 pushes the match (+ a visual + marker) to @found print "full original line with a match: $line\n"; } } for $found(@found) { say $found; } __DATA__ 123 abcde this sentence has foo bar tryna much too long for my taste + CONTAINS MATCH this doesn't have the magic phrase 123456 7890 abcd3e fc. much too long for my taste but tryno tryna foo bar baz CONTAINS MAT +CH work was put into the tryna document which shows good work CONTAINS M +ATCH problems with our out of town and other tryna that never show up CON +TAINS MATCH Tryna fill to gully and TRYNA upside of big Pine CONTAINS MATCH TWICE + BUT ... ...FAILS ON Ln 15 BECUZ THERE IS NO $word NOR ANY SPACE ... ...PRECEEDING THE FIRST INSTANCE OF $string! no searchstring here endit

And here is the output (the full lines are redundant to OP's stated needs but are included for clarity):

F:\PMonks\>1204380.pl full original line with a match: 123 abcde this sentence has foo bar +tryna much too long for my taste CONTAINS MATCH full original line with a match: much too long for my taste but tryno + tryna foo bar baz CONTAINS MATCH full original line with a match: work was put into the tryna document + which shows good work CONTAINS MATCH full original line with a match: problems with our out of town and ot +her tryna that never show up CONTAINS MATCH full original line with a match: Tryna fill to gully and TRYNA upside + of big Pine CONTAINS MATCH TWICE BUT ... match: foo bar tryna much too match: but tryno tryna foo bar match: into the tryna document which match: and other tryna that never match: gully and TRYNA upside of F:\PMonks>