CRegExp.Replacemethod
Replaces text found in a regular expression search.
Syntax
FUNCTION Replace (BYREF wszSourceString AS CONST WSTRING, BYREF wszReplaceString AS CONST WSTRING) AS DWSTRING
Parameters
| Name | Description | |
|---|---|---|
wszSourceString | The string to parse. | |
wszReplaceString | The replacement string. |
Description
Replaces text found in a regular expression search.
Remarks
The Replace method returns a copy of wszSourceString with the text replaced with wszReplaceString. If no match is found, a copy of wszSourceString is returned unchanged.
Examples
#INCLUDE ONCE "AfxNova/CRegExp.inc" USING AfxNova
DIM pRegExp AS CRegExp pRegExp.Pattern = "fox" pRegExp.IgnoreCase = TRUE DIM dwsText AS DWSTRING = "The quick brown fox jumped over the lazy dog." ' Make replacement DIM dwsRes AS DWSTRING = pRegExp.Replace(dwsText, "cat") PRINT dwsRes ' Output: The quick brown cat jumped over the lazy dog.
DIM pRegExp AS CRegExp pRegExp.Pattern = "(\S+)(\s+)(\S+)" pRegExp.IgnoreCase = TRUE DIM dwsText AS DWSTRING = "The quick brown fox jumped over the lazy dog." ' Make replacement DIM dwsRes AS DWsTRING = pRegExp.Replace(dwsText, "$3$2$1") PRINT dwsRes ' Outpput: "quick The brown fox jumped over the lazy dog."
DIM pRegExp AS CRegExp pRegExp.Global = TRUE pRegExp.Pattern = "(\d{3})-(\d{3})-(\d{4})" DIM dwsText AS DWSTRING = "555-123-4567" DIM dwsRes AS DWSTRING = pRegExp.Replace(dwsText, "($1) $2-$3") print dwsRes ' Output: "(555) 123-4567"
DIM pRegExp AS CRegExp pRegExp.Global = TRUE pRegExp.IgnoreCase = TRUE pRegExp.Pattern = "World" PRINT pRegExp.Replace("Hello World", "Earth") 'Output: "Hello Earth"
DIM pRegExp AS CRegExp pRegExp.Global = TRUE pRegExp.Pattern = "[bac]" ' --or-- pRegExp.Pattern = "b|a|c" PRINT pRegExp.Replace("abacadabra", "") ' Output: "d**r*"
DIM pRegExp AS CRegExp pRegExp.Global = TRUE pRegExp.Pattern = $"(\S+), (\S+)" PRINT pRegExp.Replace("Roca, José", "$2 $1") ' Output: "José Roca"
DIM pRegExp AS CRegExp pRegExp.Global = TRUE pRegExp.Pattern = $"\b0{1,}\." PRINT pRegExp.Replace("0000.34500044", ".") ' Output: ".34500044"
In addition, the Replace method can replace subexpressions in the pattern.
The following call to the function shown in the previous example swaps the first pair of words in the original string:
Suppose that you have a telephone directory, and all the phone numbers are formatted like this: 555-123-4567. Now, you decide that all the phone numbers should be formatted to look like this: (555) 123-4567.
What we have done is to search for 3 digits (\d{3}) followed by a dash, followed by 3 more digits and a dash, followed by 4 digits and add () to the first three digits and change the first dash with a space. $1, $2, and $3 are examples of a regular expression "back reference." A back reference is simply a portion of the found text that can be saved and then reused.
Reference
- Include file
CRegExp.inc - Defined in AfxNova/CRegExp.inc:586
- Documented in Windows/Windows Script/CRegExp Class.md
- Topic: CRegExp Class