RegExpReplace(string, string, int, int)

From NWN Lexicon
Jump to navigationJump to search
Note: This article documents Neverwinter Nights: Enhanced Edition new content or changes/updates/fixes to 1.69 functions. These are all listed under the category and patches pages.

Replaces all matching sRegExp in sValue with sReplacement.

string RegExpReplace(
    string sRegExp,
    string sValue,
    string sReplacement,
    int nSyntaxFlags = REGEXP_ECMASCRIPT,
    int nMatchFlags = REGEXP_FORMAT_DEFAULT
);


Parameters

sRegExp
The regular expression string.
sValue
A string value to operate upon with the regular expression.
sReplacement
A replacement string.
nSyntaxFlags
nSyntaxFlags is a mask of REGEXP_* constants.
nMatchFlags
nMatchFlags is a mask of REGEXP_MATCH_* and REGEXP_FORMAT_* constants.


Description

Replaces all matching sRegExp in sValue with sReplacement.

  • Returns a empty string on error.
  • Please see the format documentation for replacement patterns.
  • nSyntaxFlags is a mask of REGEXP_*
  • nMatchFlags is a mask of REGEXP_MATCH_* and REGEXP_FORMAT_*.
  • FORMAT_DEFAULT replacement patterns:
   $$    $
   $&    The matched substring.
   $`    The portion of string that precedes the matched substring.
   $'    The portion of string that follows the matched substring.
   $n    The nth capture, where n is a single digit in the range 1 to 9 and $n is not followed by a decimal digit.
   $nn   The nnth capture, where nn is a two-digit decimal number in the range 01 to 99.

Format Constants

These constants are for RegExpReplace() only, do not mix with other RegExp functions:

The first character in [first,last) will be treated as if it is not at the beginning of a line (i.e. ^ will not match [first,first)
int REGEXP_MATCH_NOT_BOL                            = 1;

The last character in [first,last) will be treated as if it is not at the end of a line (i.e. $ will not match [last,last)
int REGEXP_MATCH_NOT_EOL                            = 2;

"\b" will not match [first,first)
int REGEXP_MATCH_NOT_BOW                            = 4;

"\b" will not match [last,last)
int REGEXP_MATCH_NOT_EOW                            = 8;

If more than one match is possible, then any match is an acceptable result
int REGEXP_MATCH_ANY                                = 16;

Do not match empty sequences
int REGEXP_MATCH_NOT_NULL                           = 32;

Only match a sub-sequence that begins at first
int REGEXP_MATCH_CONTINUOUS                         = 64;

--first is a valid iterator position. When set, causes match_not_bol and match_not_bow to be ignored
int REGEXP_MATCH_PREV_AVAIL                         = 128;

Use ECMAScript rules to construct strings (http://ecma-international.org/ecma-262/5.1/#sec-15.5.4.11)
int REGEXP_FORMAT_DEFAULT                           = 0;

Use POSIX sed utility rules (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html#tag_20_116_13_03)
int REGEXP_FORMAT_SED                               = 256;

Do not copy un-matched strings to the output
int REGEXP_FORMAT_NO_COPY                           = 512;

Only replace the first match
int REGEXP_FORMAT_FIRST_ONLY                        = 1024;

Remarks

Make sure you know regular expressions before using this - else you might end up with more problems to debug then you realize.

There won't be large amounts of regular expression documentation on this wiki, find that elsewhere.


Version

This function was added in 1.87.8193.35 of NWN:EE.


Example

RegExpReplace("a+", "vaaalue", "[$&]") // => "v[aaa]lue"

See Also

functions:

RegExpIterate() RegExpMatch()

constants:

REGEXP_* REGEXP_MATCH_* REGEXP_FORMAT_*


 author: Shadguy