RegexReplace$() function - MapBasic - 2023

MapInfo MapBasic Reference

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapBasic
Version
2023
Language
English
Product name
MapBasic
Title
MapInfo MapBasic Reference
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:32:32.686312

Purpose

Replaces all/single RegEx matches in a string and returns modified string after doing a RegEx match replace. You can call this function from the MapBasic window in MapInfo Pro.

Syntax

RegExReplace$(input_string, expression_string, replace_text, 
			[, case_insensitive ] [, replace_all ] )

input_string (String): A string with the target sequence (the subject) against which the RegEx expression is matched and replaced.

expression_string (String) : The RegEx pattern to match the string to.

replace_text (String) : The string with the replacement text.

case_insensitive (Logical) : Character matching should be performed without regard to case. (This parameter is Optional) (default value is TRUE)

replace_all (Logical) : Replace all matches in string or not (This parameter is Optional) (default value is TRUE)

.

Return Value

Logical

Description

Use the RegexReplace$() function to replace all/single RegEx matches in a string. This function returns a modified string after doing RegEx match replace.

case_insensitive is optional and defaults to TRUE.

replace_All is optional and defaults to TRUE.
Note: This function uses ECMAScript grammar for RegEx match. https://www.cplusplus.com/reference/RegEx/ECMAScript/
Note: Positive/negative lookbehind regex expression are not supported.

Examples

print RegExReplace$("file name is foo.txt", "([a-z]+)(\.txt)", "$1.dat")
//returns "file name is foo.dat"
print RegExReplace$("file name is foo.txt", "([a-z]+)(\.txt)", "unknown")
//returns "file name is unknown"
print RegExReplace$("file name is foo.TXT", "([a-z]+)(\.txt)", "unknown", FALSE)
//returns "file name is foo.TXT"
print RegexReplace$("foo.txt to foo1.txt", "(\w+).txt", "$1.dat")
//returns "foo.dat to foo1.dat"
print RegexReplace$("foo.txt to foo1.txt", "(\w+).txt", "$1.dat", TRUE, FALSE)
//returns "foo.dat to foo1.txt"