Click here to Skip to main content
11,573,666 members (51,016 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 VS2008 VS2010 VB.NET Text , +
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 13-Nov-11 16:27pm
Comments
Prerak Patel at 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 at 13-Nov-11 22:47pm
   
yup exactly.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

(?!{[^{]*)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
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
  Permalink  
v2
Comments
amit_upadhyay at 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 at 13-Nov-11 23:45pm
   
Updated the answer. You can match multiple words with regex. If this helped, mark as answer.
Wayne Gaylard at 14-Nov-11 0:58am
   
Nice tutoring +5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Solution for your problem is the below method System.Text.RegularExpression namespace,

Regex.Replace

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

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web04 | 2.8.150624.2 | Last Updated 13 Nov 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100