Regexp::Subst::NoRegex - emulate s/// using s/\Q// or substr


Regexp-Subst-NoRegex documentation  | view source Contained in the Regexp-Subst-NoRegex distribution.

Index


NAME

Top

Regexp::Subst::NoRegex - emulate s/// using s/\Q// or substr

SYNOPSIS

Top

  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";
  }

DESCRIPTION

Top

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::NoRegex::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.

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;

rnr_sop ($text, $left, $right, $subsop);
subsop

This is a hash reference containing

$subsop{global}

callback routine for global substitutions

Takes arguments ($data, $left, $right) where $left should be globally substituted for $right, and $data is the data from $subsop{data}.

$subsop{single}

callback routine for sequential substitutions

Takes arguments ($data, $left, $right, $orig, $gb_list);

where $gb_list is a list of good or bad substitutions in the form ('good', 'bad', 'bad', 'good', 'good'), and $left should be substituted with either $right or $orig depending on whether $gb_list says "good" or "bad". $data is $subsop{data}.

$subsop{data}

data to send to the callback routines.

AUTHOR

Top

Ben Bullock, <benkasminbullock@gmail.com<gt>

COPYRIGHT AND LICENSE

Top


Regexp-Subst-NoRegex documentation  | view source Contained in the Regexp-Subst-NoRegex distribution.