in reply to Parse a large string
#!/usr/bin/perl -w use strict; $/=undef; my $data = <DATA>; my @text = ($data =~ /(Nullam.+(?:augue|libero)\.)/g); print @text; #__prints: #Nullam quis augue.
Update: Including grandfather's idea and cleaning up line to account for Nullam and augue or libero being on different lines (take out the \n's if any), and print each sentence on different line, see below. The [^.]+ works well here as we don't have to worry about using say /s regx modifier to allow "." to also match newlines (by default "." matches anything except a newline).
$/=undef; my $data = <DATA>; my @text = ($data =~ /(Nullam\b[^.]+(?:augue|libero)\.)/g); @text = map{tr/\n/ /;$_}@text; print join("\n",@text),"\n"; #print join(" ",@text); #alternative to put a space after the period.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parse a large string
by GrandFather (Saint) on Mar 10, 2009 at 20:40 UTC | |
by Marshall (Canon) on Mar 11, 2009 at 18:57 UTC |