VB.net nested RegEx statements.

Is it possible to nest multiple regex replaces in 1 line?

For example, I have the following:

<% Regex.Replace(Request.QueryString.ToString().Replace("&", "&amp;"),"(&|&amp;)?(LowerPrice|UpperPrice)=\d*(\.\d*)?","") %>

which is working but I need to regex replace more items out of this query string

and say I also needed to remove Affiliate=\d* and needed to remove Vendor=\d*

how would I nest those additional regex replaces in that same code line?

Thank you,
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ess KayEntrapenuerCommented:
you will just need add replace to the end for each thing you want to additionally replace


Regex.Replace(Request.QueryString.ToString().Replace("&", "&amp;"),"(&|&amp;)?(LowerPrice|UpperPrice)=\d*(\.\d*)?","").replace("Affiliate=\d*","").Replace("Vendor=\d*", ......
Ess KayEntrapenuerCommented:
what i would do to make more effecient is to make a replace function which takes the string you are using, and replace character and send it back to you

then make a List or dictionary with replace strings

then run a loop through the list

Imports System.Text.RegularExpressions

Module Example
 Dim ReplaceList As New List(Of KeyValuePair(Of String, String))
public sub main()
   ReplaceList.Add(New KeyValuePair(Of String, String)("STRING TO SEARCH", "STRING TO REPLACE"))
   ReplaceList.Add(New KeyValuePair(Of String, String)("Affiliate=\d*", ""))
   ReplaceList.Add(New KeyValuePair(Of String, String)("Vendor=\d*", ""))
   ReplaceList.Add(New KeyValuePair(Of String, String)("&", "&amp;"))

        For Each pair As KeyValuePair(Of String, String) In ReplaceList
            ' Get key.
            Dim pattern As String = pair.Key
            ' Get value.
            Dim replacement As String = pair.Value
            ' Display.
            'Console.WriteLine("{0}, {1}", pattern , replacement )
          Replacethings(pattern,YOURSTRINGTOLOOPTHROUGH  ,replacement )

end sub

   Public Sub Replacethings(byval pattern as string, byref input as string,byval replacement as string )

      Dim rgx As New Regex(pattern)
      Dim result As String = rgx.Replace(input, replacement)
      'Console.WriteLine("Original String:    '{0}'", input)
     ' Console.WriteLine("Replacement String: '{0}'", result)                             
   End Sub 
End Module

Open in new window

Hope that helps
Ess KayEntrapenuerCommented:
As for your quesion, my first answer is incorrect, because the input you replace does not change, so you will need to change it, then replace again..and so on

There is however a way to do it, but you will need to embed the replace string like this

DIM result =
Regex.replace (
                  (Regex.Replace(Request.QueryString.ToString().Replace("&", "&amp;"),"(&|&amp;)?    (LowerPrice|UpperPrice)=\d*(\.\d*)?","")


BAsically, you will be using the result from your previous replace, as the input for the next one.

But i would use a function - it is much cleaner

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smowerAuthor Commented:
Thank you! I tried this solution without the dim result =
like this <%= Regex.Replace( Regex.Replace(Request.QueryString.ToString(),"(&|&amp;)?(LowerPrice|UpperPrice)=\d*(\.\d*)?",""),"Affiliate=\d*","") %>

and it seems to work properly.

Thank you,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.