Skip to content

Instantly share code, notes, and snippets.

@arodland
Created September 30, 2009 09:46
Show Gist options
  • Save arodland/197947 to your computer and use it in GitHub Desktop.
Save arodland/197947 to your computer and use it in GitHub Desktop.
package Regexp::NegativeLookBehind;
use strict;
use warnings;
=head1 SYNOPSIS
my $assertion = Regexp::NegativeLookBehind->not_after(
'neplusultra',
'tough act to follow'
);
$str =~ /${assertion}whew!/;
=cut
# XXX replace this with something that's not totally stupid
sub get_repr {
my $self = shift;
use Data::Dumper;
local $Data::Dumper::Indent = 0;
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Useqq = 1;
local $Data::Dumper::Deparse = 0;
return Dumper(\@_);
}
sub not_after {
my $class = shift;
my $re_string = '(?(?{' . $class . '->assert_after(' . $class->get_repr(@_) . ') })(?!))';
use re 'eval';
return qr/$re_string/;
}
sub not_after_inline {
my $class = shift;
my $re_string = '(?(?{ my $r=0; for my $f (@{' . $class->get_repr(@_) . '}) {'
. '$r=1, last if substr($_, pos()-length $f, length $f) eq $f}$r})(?!))';
use re 'eval';
return qr/$re_string/;
}
sub assert_after {
for my $reject (@{$_[1]}) {
return 1 if substr($_, pos() - length $reject, length $reject) eq $reject;
}
return 0;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment