Re: Interesting little regex

"Alan Young" <[email protected]>
Newsgroups gmane.org.user-groups.linux.provo.plug,gmane.comp.lang.perl.fun
Message-ID <[email protected]>
I know, replying to myself.

Parsing the KJV Bible took about 7 seconds with this:

#!/usr/bin/perl -w

use strict;

my $text = do {
  open my $T, '<./kjv10.txt' or die "Couldn't open kjv10.txt: $!\n";
  local $/;
  <$T>;
};

my %unique;

$text =~ s{(
             (\b\w+(?:['-]+\w+)*\b)
             (??{!$unique{$^N}++?"(?=)":"(?!)"})
           )
          }{
           $1
          }xg;

print "$_ => $unique{$_}\n" for sort keys %unique;

(It was pointed out that that's not a completely fair timing ... we
had to load the 4 M file into memory.)
--
Alan

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.