#!/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, followed by a space my (@slurp, $line, @found, $found); # declare vars; bad practice to do as globals, but simpler to read @slurp = ; # 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 $string 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 MATCH work was put into the tryna document which shows good work CONTAINS MATCH problems with our out of town and other tryna that never show up CONTAINS 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