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,
Shawn
smowerAsked:
Who is Participating?
 
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(
                  (Regex.Replace(Request.QueryString.ToString().Replace("&", "&amp;"),"(&|&amp;)?    (LowerPrice|UpperPrice)=\d*(\.\d*)?","")
          ,   {REPLACEMENT VALUE HERE})  'SECOND TIER REPLACE HERE
 ,   {REPLACEMENT VALUE HERE}   'THIRD TIER REPLACE HERE ..ETC
)

 %>




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
0
 
Ess KayEntrapenuerCommented:
you will just need add replace to the end for each thing you want to additionally replace

IE

Regex.Replace(Request.QueryString.ToString().Replace("&", "&amp;"),"(&|&amp;)?(LowerPrice|UpperPrice)=\d*(\.\d*)?","").replace("Affiliate=\d*","").Replace("Vendor=\d*", ......
0
 
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.Clear()
   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;"))
        
DIM YOURSTRINGTOLOOPTHROUGH as string

        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 )
        Next

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)
input=result
      'Console.WriteLine("Original String:    '{0}'", input)
     ' Console.WriteLine("Replacement String: '{0}'", result)                             
   End Sub 
End Module

Open in new window



Hope that helps
0
 
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,
Shawn
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.