On Fri, Jun 11, 2004 at 10:08:23AM -0500, James Edward Gray II wrote:
> On Jun 10, 2004, at 9:46 PM, Beau E. Cox wrote:
>
> >Hi -
> >
> >I am trying to come up with a simple, elegant word parsing script,
> >that:
> >
> >* takes a scalar string, and
> >* splits it into words separating on white space, commas,
> > and a set of delimiters: "" '' // () {} [] ##, and
> >* returns the array of words.
Personally, I would do this:
#!/usr/bin/perl
use Text::ParseWords;
my $delims = qr!["'{}[\]#\s]!;
my $line = 'This is#a#test\'of\'the"quotewords"function{with}separators[of
many]kinds';
my @words = "ewords($delims, 0, $line);
$i = 0;
for (@words) {
print "$i: <$_>\n";
$i++;
}
Which outputs the following:
0: <This>
1: <is>
2: <a>
3: <test>
4: <of>
5: <the>
6: <quotewords>
7: <function>
8: <with>
9: <separators>
10: <of>
11: <many>
12: <kinds>
Note that, since you are splitting on ', contractions and possessives
will be mangled.
--Dks
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>