NAME
Regexp::Subst::NoRegex - emulate s/// using s/\Q// or substr
SYNOPSIS
use Regexp::Subst::NoRegex qw/rnr_substr rnr_sop/;
my $text = "ajim jam jom ejeme";
my $copy = $text;
$copy =~ s/\bj(.)(.)/$2 $1/g;
my $copy1 = rnr_substr ($text, '\bj(.)(.)', '$2 $1');
my $copy2 = rnr_sop ($text, '\bj(.)(.)', '$2 $1');
if ($copy eq $copy2 && $copy eq $copy1) {
print "OK\n";
} else {
print "Oh no, more bugs.\n";
}
DESCRIPTION
Given $text
, a regex $left
and a right hand side $right
, perform the substitution $text =~ s/$left/$right/g;
without using regular expressions for the substitution operation.
The module contains two different algorithms. One, rnr_substr, uses substr
to perform the substitutions, and the other, rnr_sop, uses multiple non-regex substitutions of the form $text =~ /\Q$x/$y/
to emulate Perl's regex substitution while actually switching it off with the \Q.
EXPORT
Exports rnr_substr
(substr version) and rnr_sop
(s/\Q// version) on request.
$verbose
Set
$Regexp::Subst::Substr::verbose = 1;
to see what the module is doing.
rnr_substr
Regular expression substitution without using any regular expressions (rnr stands for regex no regex) - emulate
$text =~ s/$left/$right/g;
with a series of substitutions which use Perl's built in function substr
substr ($text, $position, $length) = $y;
- rnr_substr ($text, $left, $right);
- text
-
The text you want to substitute
- left
-
Any LHS expression, including regular expressions.
- right
-
Any RHS expression, including things like
$1
,$2
, etc.
rnr_sop
Regular expression substitution without using any regular expressions (rnr stands for regex no regex) - emulate
$text =~ s/$left/$right/g;
with the subsitution operator (s///) with regexes switched off.
$text =~ s/\Q$x/$y/g;
AUTHOR
Ben Bullock, <benkasminbullock@gmail.com<gt>
COPYRIGHT AND LICENSE
Copyright (C) 2008 by Ben Kasmin Bullock.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.