Looking for RegEx examples you can drop straight into your project? This practical cheat sheet focuses on the most searched tasks, such as find/replace, extracting values, lookahead/lookbehind, and non-greedy matches, using Xojo’s RegEx class with concise, ready-to-run snippets. Copy, paste, ship.
Setup (3 lines you’ll reuse)
Var re As New RegEx
Var opt As New RegExOptions
re.Options = opt
1) Replace all occurrences (word boundary)
Goal: a → the (whole word)
Var re As New RegEx
Var opt As New RegExOptions
opt.ReplaceAllMatches = True
re.Options = opt
re.SearchPattern = "\ba\b"
re.ReplacementPattern = "the"
MessageBox(re.Replace("a bus on a street in a town"))
' the bus on the street in the town
2) Strip HTML tags
Var re As New RegEx
Var opt As New RegExOptions
opt.ReplaceAllMatches = True
re.Options = opt
re.SearchPattern = "<[^<>]+>"
re.ReplacementPattern = ""
MessageBox(re.Replace("<p>Hello <b>world</b>.</p>"))
' Hello world.
3) Extract the first number (capture groups)
re.SearchPattern = "(\d+)"
Var m As RegExMatch = re.Search("Order #12345 shipped")
If m <> Nil Then MessageBox(m.SubExpressionString(1)) ' 12345
4) Reformat dates with $1, $2, $3
Goal: YYYY-MM-DD → DD/MM/YYYY
Var re As New RegEx
Var opt As New RegExOptions
opt.ReplaceAllMatches = True
re.Options = opt
re.SearchPattern = "(\d{4})-(\d{2})-(\d{2})"
re.ReplacementPattern = "$3/\/\"
MessageBox(re.Replace("Due: 2025-09-09 and 2025-12-31"))
' Due: 09/09/2025 and 31/12/2025
5) Find all words (simple iterator)
re.SearchPattern = "\w+"
Var match As RegExMatch = re.Search("one two three")
While match <> Nil
System.DebugLog(match.SubExpressionString(0))
match = re.Search ' continues from last position
Wend
6) Replace only the second occurrence
Var s As String = "a bus drove on a street in a town"
re.SearchPattern = "\ba\b"
re.ReplacementPattern = "the"
Var first As RegExMatch = re.Search(s) ' find first
If first <> Nil Then
s = re.Replace ' replaces the next match only
End If
MessageBox(s)
' a bus drove on the street in a town
7) Lookarounds
Match “foo” not followed by “bar”
re.SearchPattern = "foo(?!bar)"
7.1 Negative lookahead: match only http (not https) URLs
Var re As New RegEx
re.SearchPattern = "http(?!s)://\S+" ' http:// not followed by s
Var txt As String = "http://a.com and https://b.com"
Var m As RegExMatch = re.Search(txt)
While m <> Nil
System.DebugLog(m.SubExpressionString(0)) ' http://a.com
m = re.Search
Wend
7.2 Positive lookbehind + optional decimals: amounts right after $
Var re As New RegEx
re.SearchPattern = "(?<=$)\d+(?:\.\d{2})?" ' $123 or $123.45 → capture just the number
Var txt As String = "Total: $19.99, Tax: $2"
Var m As RegExMatch = re.Search(txt)
While m <> Nil
System.DebugLog(m.SubExpressionString(0)) ' 19.99, 2
m = re.Search
Wend
Tip:
- Fixed‑width only for look‑behind: avoid quantifiers like +, * inside (?<=…)/(?<!…).
- Use lookarounds when you need context to the left/right but don’t want it included in the match or replacement.
8) Smallest span between tags (non‑greedy)
re.SearchPattern = "<b>.+?</b>" ' or: opt.Greedy = False
9) Extract emails (simple demo)
re.SearchPattern = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}"
Var em As RegExMatch = re.Search("Contact: me@example.com")
If em <> Nil Then MessageBox(em.SubExpressionString(0)) ' me@example.com
10) Start at character position N (UTF‑8 safe)
Var t As String = "ąβç sample"
Var charPos As Integer = 5 ' 1-based character index
re.SearchPattern = "\w+"
re.SearchStartPosition = t.Left(charPos - 1).Bytes ' convert char index → byte offset
Var mm As RegExMatch = re.Search(t)
If mm <> Nil Then System.DebugLog(mm.SubExpressionString(0))
Quick replacement references
- $& = entire match (This reference represents the exact piece of text that your regular expression found. It’s incredibly useful if you want to add something before or after the matched text without changing the match itself.)
- $1…$50 = captured groups (Regular expressions allow you to define specific “groups” within your match by enclosing parts of your pattern in parentheses
()
. Each set of parentheses creates a captured group.) - $` = text before the match (This special reference represents all the text in your original string that appears before the part your regular expression matched. It’s handy if you want to keep the beginning of the string intact while making a change in the middle.)
- $’ = text after the match (This refers to all the text in your original string that comes after the current match. It’s useful for preserving the end of the string.)
Options you’ll use most
opt.ReplaceAllMatches = True ' global replace
opt.CaseSensitive = True ' default is False
opt.DotMatchAll = True ' make . match newlines
opt.Greedy = False ' prefer shortest matches
opt.TreatTargetAsOneLine = True ' ^ and $ match whole string
Common gotchas
- Greediness: “.+” can overmatch; use .+? or set Greedy = False.
- Case sensitivity: default searches are case‑insensitive; enable CaseSensitive when exact case matters.
- UTF‑8: SearchStartPosition is byte‑based; convert character index to bytes (see example ↑).
- Lookbehind must be fixed‑width.
- Iteration: After the first Search(targetString), call re.Search with no args to continue.
Wrap‑up
Got a tricky pattern, validation, parsing, or a lookaround edge case you want covered? Drop a comment in the forums with your use.
Happy matching!
Gabriel is a digital marketing enthusiast who loves coding with Xojo to create cool software tools for any platform. He is always eager to learn and share new ideas!