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>

In reply to Re^2: question about finding strings (regexes and slurping files) by ww
in thread question about finding strings? by SaraBetsy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.