Regex Replace

Why doesn’t this match properly?

[code] Dim re As New RegEx
re.SearchPattern = “a”
re.ReplacementPattern = “the”
re.Options.ReplaceAllMatches = True

Dim t As String = “a bus drove on a street in a town”

Dim match As RegExMatch = re.Search(t)
If match <> Nil Then
t = re.Replace()
End If
break[/code]

This outputs a bus drove on the street in the town

Why doesn’t it return the bus drove on the street in the town ?

I originally wanted to replace this:

[code] dim t1 as string
t="<part1.part2.part3>"

re.SearchPattern = “part1.part2.part3”
re.ReplacementPattern = “newpart1.newpart2.newpart3”
re.Options.ReplaceAllMatches = True
match = re.Search(t)
If match <> Nil Then
t1 = re.Replace()
End If
break[/code]

But this did not work, so tried the example. The code DOES go inside the match if block.

FYI using MBS plugins I get what I ask for

Xojo 2015r2.4 & Xojo 2015r3

You need:

t = re.Replace(t)

Not giving a parameter for search() or replace() means “search or replace starting from the last place I searched or replaced.”

In your code you’re searching t, it finds the first occurrence of “a” and then by saying replace() instead of replace(t) it’s starting the replacement after the first occurrence.

You may not need to check for a match. (Unless you want to know if you replaced something or not.) Just do:

[code]Dim re As New RegEx
re.SearchPattern = “a”
re.ReplacementPattern = “the”
re.Options.ReplaceAllMatches = True

Dim t As String = “a bus drove on a street in a town”
t = re.Replace(t)

break[/code]

Aha!, you are right

Thanks