Link to home
Start Free TrialLog in
Avatar of dodgerfan
dodgerfanFlag for United States of America

asked on

Update old ASP RegEx function for use in ASP.Net

I have an old regular expression function written for a traditional ASP webpage that I'm trying to translate for an ASP.Net application. I do not have any experience with regular expressions. The function is below. Any idea how I can get it working for ASP.Net (C# code behind). Thanks for any help/guidance. The search is a full text search against a SQL Server database.

if strSearch_len < 1 then
      Response.Write "Enter a search string"
      Response.End
else
      dim con
      dim rs, strSearch

strIn = strSearch
set RegEx = New RegExp
RegEx.IgnoreCase = True
RegEx.Global = True

RegEx.Pattern = "(\s{2,})"      // 2 or mor spaces
strIn = RegEx.Replace(strIn, " ")  //replace 2 or more spaces with one

RegEx.Pattern = "(\b[a-zA-Z0-9_\-\']*)\b|(?:"")([\w\W]*)(?:""))"
If RegEx.Test(strIn) then
      strIn = RegEx.Replace(strIn, Chr(34) & "$1" & Chr(34))
End If

RegEx.Pattern = "(""{2,})"      // 2 or more quotes
strIn = RegEx.Replace(strIn, """")  // replace 2 or more quotes

RegEx.Pattern = "("",\s"")"
If RegEx.Test(strIn) then
      strIn = RegEx.Replace(strIn, """ OR """)
End If

RegEx.Pattern = "(""\s?&\s?"")"
If RegEx.Test(strIn) then
      strIn = RegEx.Replace(strIn, """ AND """)
End If

strIn = Replace(strIn, """and""", "AND")
strIn = Replace(strIn, """not""", "NOT")
strIn = Replace(strIn, """or""", "OR")
strIn = Replace(strIn, """) """,""") OR """)
strIn = Replace(strIn,")"" OR ""(",") OR (")
strIn = Replace(strIn,")"" AND ""(", ") AND (")

RegEx.Pattern = "(""\s"")"
If RegEx.Test(strIn) then
      strIn = RegEx.Replace(strIn, """ OR """)
End If

RegEx.Pattern = "("""")"
If RegEx.Test(strIn) then
      strIn = RegEx.Replace(strIn, chr(34))
End If

RegEx.Pattern = "("" "")"
If RegEx.Test(strIn) then
      RegEx.Replace(strIn, """ OR """)
End If

strIn = Replace(strIn,"", "''")

End If
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dodgerfan

ASKER

Based on that example, I came up with this:

protected string FixInput(string searchArg)
{
      string input = searchArg;
      string pattern = "\\s+";
      string replacement = " ";
      string result = Regex.Replace(input, pattern, replacement);

      return result
}

This removes extra whitespace. How do you add in more regular expressions, i.e, I want it to do other things to the string than just whitespace removal, such as replace characters or certain words. Thanks for the links, too.
It should work the same way. Adjust the pattern and replacement variables accordingly.