Goto main content

resub

Perform a regular expression substitute.

RESUB

NAME

resub - Performs a regular expression substitute.

SYNOPSIS

resub("string", "pattern", "substitute" [, "options"]);

DESCRIPTION

Resub matches the pattern specified by the second parameter on the string specified by the first parameter and replaces it with the third parameter.

Resub is based on the library pcrs.

The substitute uses Perl syntax as documented in the perlre(1) manual page, with some exceptions:

Most notably and evidently, since PCRS is not Perl, variable interpolation or Perl command substitution won’t work. Special variables that do get interpolated, are:

$1, $2, …, $n Like in Perl, these variables refer to what the nth capturing subpattern in the pattern matched.

$& and $0 refer to the whole match. Note that $0 is deprecated in recent Perl versions and now refers to the program name.

$+ refers to what the last capturing subpattern matched.

$‘ and $’ (backtick and tick) refer to the areas of the subject before and after the match, respectively. Note that, like in Perl, the unmodified subject is used, even if a global substitution previously matched.

Perl4-style references to subpattern matches of the form \1, \2, … which only exist in Perl5 for backwards compatibility, are not supported.

Also, since the substitute is a double-quoted string in Perl, you might expect all Perl syntax for special characters to apply. In fact, only the following are supported:

\n     newline (0x0a)
\r     carriage return (0x0d)
\t     horizontal tab (0x09)
\f     form feed (0x0c)
\b     backspace (0x08)
\a     alarm, bell (0x07)
\e     escape (0x1b)
\0     binary zero (0x00)

Options The options gmisx are supported. e is not, since it would require a Perl interpreter and neither is o, because the pattern is explicitly compiled, anyway. Additionally, PCRS honors the options U and T. Where PCRE options are mentioned below, refer to PCRE(3) for the subtle differences to Perl behaviour.

g      Replace all instances of pattern in subject, not just the first one.
i      Match the pattern without respect to case. This translates to PCRE_CASELESS.
m      Treat the subject as consisting of multiple lines, i.e.  ’^’ matches immediately after, and ’$’ immediately before each newline.  Translates to PCRE_MULTILINE.
s      Treat the subject as consisting of one single line, i.e.  let the scope of the ’.’ metacharacter include newlines.  Translates to PCRE_DOTALL.
x      Allow extended regular expression syntax in the pattern, enabling whitespace and comments in complex patterns.  Translates to PCRE_EXTENDED.
U      Switch the default behaviour of the ’*’ and ’+’ quantifiers to ungreedy. Note that appending a ’?’ switches back to greedy(!).  The explicit in-pattern switches      (?U)  and  (?-U)  remain  unaf-
       fected.  Translates to PCRE_UNGREEDY.
T      Consider  the  substitute trivial, i.e. do not interpret any references or special character escape sequences in the substitute. Handy for large user-supplied        substitutes, which would otherwise have to be examined and properly quoted.
Unsupported options are silently ignored.

NOTE

Resub does not work well with utf-8 characters. If you want to replace characters to get a valid valid name, use the function esc with the filter filenameutf8.

EXAMPLES

Note: In the followings examples, the _ between the { should be removed to make it work.

res={_{
            a = "Pierre Laplante";
            a.resub("i","x");
            a;
        }}. return res=Pxerre LaplantePierre Laplante.
res={_{
            a = "Pierre Laplante";
            a.resub("e","x");
        }}. return res=Pixrre Laplante.
res={_{
            a = "Pierre Laplante";
            a.resub("e","x","g");
        }}. return res=Pixrrx Laplantx.
res={_{
            a = "Pierre Laplante";
            a.resub("p","x","gi");
        }}. return res=xierre Laxlante.
res={_{
            a = "Pierre Laplante";
            a.resub("([^ ]+) (.*)","$2 $1");
        }}. return res=Laplante Pierre.

SEE ALSO

{{ include("includes/strings.sn") }}

AUTHOR

Written by Pierre Laplante and Caroline Laplante, <laplante@sednove.com>

MODIFICATIONS

1.0 2014-09-09 21:24:14 laplante@sednove.com

Edit

© 2024 extenso Inc. All rights reserved.