Consulting

Results 1 to 2 of 2

Thread: Remove encoding from an xml file using regular expressions

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Posts
    67
    Location

    Remove encoding from an xml file using regular expressions

    Hi everyone,

    I want "encoding="XX"" removed from a string and I'll be using regular expressions.Where XX could be any possible encoding: UTF-8, UTF-16, ISO...

    Here is a sample:
    <?xml version="1.0" encoding="UTF-8"?>
    And this is how it should look like after the replacement:
    <?xml version="1.0"?>
    How would the pattern look like?

    Note that I'm not asking how to use regular expressions but how to build a pattern.
    David Delcor


  2. #2
    VBAX Regular
    Joined
    Jun 2007
    Posts
    67
    Location
    Code snipet based on a piece of MSDN documentation.

    [VBA]Set objRegExp = New RegExp

    ' myPattern = "encoding=" & Chr(34) & "[+]" & Chr(34)

    myPattern = "encoding=" & "\w+"
    'Set the pattern by using the Pattern property.
    objRegExp.Pattern = myPattern

    ' Set Case Insensitivity.
    objRegExp.IgnoreCase = True

    Set objRegExp = New RegExp


    ' myPattern = "encoding=" & Chr(34) & "UTF-8" & Chr(34) ==> This works fine but is a subcase of what I want.
    ' myPattern = "encoding=" & Chr(34) & "[+]" & Chr(34) ==> jumps the test if statement below
    myPattern = "encoding=" & "\w+" ==> So does that one

    'Set the pattern by using the Pattern property.
    objRegExp.Pattern = myPattern

    ' Set Case Insensitivity.
    objRegExp.IgnoreCase = True

    'Set global applicability.
    objRegExp.Global = True

    'Test whether the String can be compared.
    If (objRegExp.Test(firstLine) = True) Then

    'Replace the matches.
    firstLine = objRegExp.replace(firstLine, "") ' Replace

    End If
    [/VBA]



    JScript and VBScript regex syntax appears to be different.

    Some documentation here:

    http://msdn.microsoft.com/en-us/libr...b2(VS.85).aspx
    David Delcor


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •