Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: , +
Hello, i want to create a regex to be able to replace a string with another string while also making sure that a particular pattern is avoided from being replaced.

Eg : string is - I wanted to buy a house and this house is so good that i will {buy|purchase|rent|acquire} this place

The application should replace buy with purchase but not in the brackets. I mean all the occurences of buy should be replaced with purchase but ignore the ones that fall under {}.

The regex that i build for the brackets is this :

Dim regex As New Regex("{([^{}]+)}")


but please help me to create a regex where i can replace string and also exclude the above pattern.

the two individual expression would be like :

rgx = New Regex("buy", RegexOptions.IgnoreCase)

Dim regex As New Regex("{([^{}]+)}")


how do i merge the two to include first and exclude second one?
Posted
Comments
Prerak Patel 13-Nov-11 22:44pm    
You mean, the output should be - I wanted to purchase a house and this house is so good that i will {buy|purchase|rent|acquire} this place?
amit_upadhyay 13-Nov-11 22:47pm    
yup exactly.

(?!{[^{]*)buy(?![^{}]*})
This is what you want. This regex will get you the buy word not enclosed in {}.

You can match multiple words like (?!{[^{]*)buy|purchase(?![^{}]*})
You can do it like
VB
Dim rgx As New Regex("(?!{[^{]*)buy|purchase(?![^{}]*})")
Dim result As String = rgx.Replace(input, replacement) 'where input will be your content and replacement will be the word to be placed
 
Share this answer
 
v2
Comments
amit_upadhyay 13-Nov-11 23:33pm    
hey thanks. it works great. But iam stuck with implementing this into my main program which is a slightly modified :

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 & "(?![^{}]*})", RegexOptions.IgnoreCase)
Dim z As Integer = 0

rpl = rgx.Replace(rpl, "zzzzxxxxzzzz")

Catch ex As Exception

End Try
End If


Loop

fil2.Close()

RichTextBox1.Text = rpl


currently do you see anything which can be wrong with this code? What it is doing is reading a text file, splitting the line at "|" and then count the number of words in first segment. Then it will create a pattern dynamically including the split word instead of "buy". The output is not what i want.

with the above string i get :

zxzxzxzxzxzxzxzxzx zxzxzxzxzxzxzxzxzx zxzxzxzxzxzxzxzxzx to zxzxzxzxzxzxzxzxzx buy zxzxzxzxzxzxzxzxz zxzxzxzxzxzxzxzxzx xzxzxzxzxzxzxzxzx xzxzxzxzxzxzxzxzx ho zxzxzxzxzxzxzxzxzx zxzxzxzxzxzxzxzxzx zxzxzxzxzxzxzxzxzx d zxzxzxzxzxzxzxzxzx this zxzxzxzxzxzxzxzxzx house zxzxzxzxzxzxzxzxzx is zxzxzxzxzxzxzxzxz zxzxzxzxzxzxzxzxzx xzxzxzxzxzxzxzxz zxzxzxzxzxzxzxzxzx xzxzxzxzxzxzxzxzx good zxzxzxzxzxzxzxzxzx that zxzxzxzxzxzxzxzxzx i zxzxzxzxzxzxzxzxzx will zxzxzxzxzxzxzxzxzx {buy|purchase|rent|acquire} zxzxzxzxzxzxzxzxzx this zxzxzxzxzxzxzxzxzx place
Prerak Patel 13-Nov-11 23:45pm    
Updated the answer. You can match multiple words with regex. If this helped, mark as answer.
Wayne Gaylard 14-Nov-11 0:58am    
Nice tutoring +5!
Solution for your problem is the below method System.Text.RegularExpression namespace,

C#
Regex.Replace


More details at: http://msdn.microsoft.com/en-us/library/h0y2x3xs.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900