If your one long string is coming from a file, you don't have to use a new line to divide it up into sentences. You can set $/ to any string, including a period - see perlvar for details. This has the additional advantage of not consuming large amounts of memory by trying to slurp in a long chunk of text without any new lines all at once.
If the one long string is coming from elsewhere, you can still make use of $/ by opening an input stream on a string. Here's an example of a solution that lets you freely choose the starting words, ending words, and end of sentence marker. It uses both $/ and a string turned into an input stream.
use strict; use warnings; sub findWords { my ($sText, $sEndSentence, $reStart, $reEnd)=@_; local $/=$sEndSentence; open(LONG_STRING, "<", \$sText) || die "Can't open string"; while (my $line = <LONG_STRING>) { chomp $line; if (($line =~ $reStart) && ($line =~ $reEnd)) { print "match: $line\n"; } else { print "no match: $line\n"; } } } findWords("a b c. Nullam est libero.Nullam est augue", '.' , qr(^\s*Nullam) , qr((?:libero|augue)\s*$));
Best, beth
In reply to Re: Parse a large string
by ELISHEVA
in thread Parse a large string
by 1001jrlight
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |