Re: Interesting little regex

[email protected] ("Alan Young")
Newsgroups perl.fwp
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
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.