Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Using Regex With VBScript Error

Last post 11-10-2008, 10:39 AM by putercoder. 2 replies.
Sort Posts: Previous Next
  •  11-09-2008, 10:49 AM 48130

    Using Regex With VBScript Error

    I am trying to use a regex pattern to remove an unwanted " from a csv file. When I run the script the compiler says that there is a problem with the declaring of the regex pattern.

    it seems to break at   regEx.Pattern =  "("[^,"]+)"([^,"]+")"    when  complied. How do you set the reference of the patttern to the variable?

     ("[^,"]+)"([^,"]+") This is the regex pattern I am using with "$1$2" for the replace.

     This is running on a XP environment.

    Here is an example of the code I am working with:

    Option Explicit

    Dim regEx,newstr3, mypat
    Dim fPOSIn
    Dim fPOSOut
    Dim cArray
    Dim fso, inF, outF, sLine

    dim mylen,sLine2

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set inF = fso.OpenTextFile(fPOSIn, 1)
    fPOSIn = "D:\Webinv\POSFile\PosDropOff\POSTEMP\Barneys_858_O_20081105.csv"


    Do Until inF.AtEndOfStream
    sLine = inF.ReadLine
    sLine2 = sLine

    Set regEx =  CreateObject("vbscript.regexp")
      regEx.Pattern =  "("[^,"]+)"([^,"]+")"           
      regEx.IgnoreCase = True 
      regEx.Global = True
    Line2 = regEx.Replace("("[^,"]+)"([^,"]+")", "$1$2")

    loop

    set inF = nothing
    set outF = nothing
    Set fso = nothing
     

     

    Freddy

  •  11-09-2008, 1:10 PM 48133 in reply to 48130

    Re: Using Regex With VBScript Error

    Your VBScript syntax is bad.  http://regexadvice.com/blogs/mash/archive/2005/05/18/934.aspx

    Your pattern is conflicting with the language. In VBScript strings are bounded by quotation marks. If a string is supposed to contain a quotation mark it has to be escaped. This is just basic syntax rules has nothing to do with Regexes. The quotation in your pattern terminates the the string that  your are defining the regex. So your pattern is only the open parenthesis. Everything after that is garbage to the VBScript interpreter.

     In VBScript if you need a quotation mark in your string you escape it by using magic quotes which are two consecutive quotes within the string

    Alternatively  you can substitute the quotation mark in your Regex pattern with with \x22 which will make it more portable across environments

    Also to set your regex use 

    Set regex = New Regexp

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-10-2008, 10:39 AM 48157 in reply to 48133

    Re: Using Regex With VBScript Error

    Michael

    Thanks so much for pointing that Out to me. It is nice to know that there are people who can assist us who make many mistakes

     

     

    Freddy

View as RSS news feed in XML