I am trying to build an application which does the following :
1) write some text in a richtextbox
2) when user clicks a button, the app will replace the text with another text in {} braces.
what I want is that the next time, the regex searches for any text it should exclude those which are present in {}.
for eg :
my world is good world and a happy world and will be a better world for everyone.
first pass – change word “world”
my {world|happy place|home} is good {world|happy place|home} and a happy {world|happy place|home} and will be a better {world|happy place|home} for everyone.
second pass – change word “happy”
in this pass I want the regex to ignore all instances which are surrounded by {}. notice word happy comes in {world|happy place|home}, but it should not be touched.
now could anyone please help me with building this regex?
In simple terms I want to include the word and exclude everything that falls between {}. Also please note that word can be a string.
for eg : sometimes I might want to replace the string “happy place”.
any other logic other than regex can also help.
my code is :
Dim fil2 As New StreamReader("new2.txt")
Dim i As Integer = 0
Dim start2 As Integer = 0
Dim rgx As Regex
Dim rpl As String = RichTextBox1.Text
Do While fil2.Peek > -1
Dim StringToCheck As String = fil2.ReadLine()
Dim prev As String = StringToCheck.Split("|")(0).Trim()
If (StringToCheck.Split("|")(0).Split(" ").Count >= 2) Then
Try
rgx = New Regex("(?<=(^|})[^{]*)" & prev & "(?=[^}]*($|{))")
Dim z As Integer = 0
rpl = rgx.Replace(rpl, "{" & StringToCheck & "}")
Catch ex As Exception
End Try
End If
Loop
fil2.Close()
rpl = rpl.Replace("{? ?|? ?|? ! ?}", " ")
RichTextBox1.Text = rpl
MsgBox("done")
You can use lookaheads and lookbehinds: