UserGuide

Regular Expressions

From Xojo Documentation

Regular Expressions (RegEx) are patterns that describe text. You can use regular expressions to find substrings in text and to make replacements within text.

These are common characters used to create RegEx patterns:

Matching Character Description
. wildcard, matches any character except

/r and /n

\r return
\n newline
\d Matches a string of digits
\w Matches a string of word characters
\s Matches whitespace
* Repeat pattern zero or more times
+ Repeat pattern one or more times

Searching

Your apps can search and replace text using regular expressions (often called RegEx), a pattern describes specific text to find in a string. You use the properties of the RegEx, RegExOptions and RegExMatch classes to define a regular expression and search or replace text using regular expressions. Regular Expressions can be a bit tricky to get the hang of, but they are fast and efficient ways to process text.

This code finds the first word in the text “Software development made easy”, returning “Software”:

Dim re As New RegEx
Dim match As RegExMatch

re.SearchPattern = "\w*"
match = re.Search("Software development made easy")
If match <> Nil Then
MsgBox(match.SubExpressionString(0))
End If

Here are some examples of other search patterns on the same text "Software development made easy":

Pattern Result
made made
simple Text Not Found
^. S
[wb]are ware
..sy$ easy

Remember that a RegEx result (RegExMatch) may return more than one match. You should check RegExMatch.SubExpressionCount to see how many matches were returned.

Replacement

Regular Expressions can also be used to replace strings in text. This code replaces “Software development” with “Programming”:

Dim re As New RegEx

re.SearchPattern = "Software development"
re.ReplacementPattern = "Programming"
Dim result As String
result = re.Replace("Software development made easy")

MsgBox(result)

The result variable now contains “Programming made easy”. This removes HTML tags from source HTML:

Dim re As New RegEx
re.SearchPattern = "<[^<>]+>"
re.ReplacementPattern = ""

Dim html As String = "<p>Hello.</p>"
Dim plain As String = re.Replace(html)

While (StrComp(html, plain, 0) <> 0)
html = plain
plain = re.Replace
Wend

MsgBox(plain)

See Also

RegEx, RegExOptions, RegExMatch classes; UserGuide:Framework topic