DIAdem Help

Content Type
Programming Language
Current manual
Table of Contents

Method: Replace for RegExp

Method: Replace for RegExp

Replaces the text pattern found in a search with regular expressions.

sReplace = Object.Replace(sourceString, replaceVar)
ObjectRegExp
Object with this method
sourceStringString
Specifies the character string you want to search through.
replaceVarVariant
Specifies the text to replace the original text.
sReplaceString
Receives the character string with the replacement.

The following example specifies the text pattern bc and replaces it in the entire character string:

Dim RegExpression, sText
Set RegExpression = CreateObject("VBScript.RegExp")
RegExpression.Pattern = "bc"
RegExpression.Global = TRUE
RegExpression.IgnoreCase = TRUE
sText = RegExpression.Replace("abcABC","bcdef")
Call MsgBox(sText) ' Returns "abcdefAbcdef"

The following example searches a pattern that consists of one or more alphabetical characters, and saves them as submatch. The second part of the text pattern consists of a reference to this submatch. The text pattern searches double words and replaces them with the submatch. Using $1 in the replace method refers to the first stored submatch:

Dim RegExpression, sText
Set RegExpression = CreateObject("VBScript.RegExp")
RegExpression.Pattern = "\b([a-z]+) \1\b"
RegExpression.Global = TRUE
RegExpression.IgnoreCase = TRUE
sText = "one one two three three"
sText = RegExpression.Replace(sText,"$1")
Call MsgBox(sText) ' Returns "one two three"

See Also

Objects Overview

Related Topics

Filter | InStr | InStrB | InStrRev | Join | LCase | Left | LeftB | Len | LenB | LTrim | Mid | MidB | Replace | Right | RightB | RTrim | Space | Split | StrComp | String | StrReverse | Trim | UCase

In This Section
Was this information helpful?