Forum Discussion

ChristopheC's avatar
ChristopheC
Contributor
14 years ago

Suppression with Regular Expression

I have an XML file and I want to delete
everything that is between 2 tags including the tags.



<M_CODE>

<src>

...

</ SRC>

</ M_CODE>





I wanted to use the
Replace command but I can not use a regular expression



Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Users\TestAdmin\Desktop\Essai.XML", 1)



strText = objFile.ReadAll

objFile.Close

RegExpr = "^<M_CODE>(.+)</M_CODE>$"

strNewText = Replace(strText, RegExpr, "")



Set objFile = objFSO.OpenTextFile("C:\Users\TestAdmin\Desktop\Essai.XML", 2)

objFile.WriteLine strNewText

objFile.Close





or
should I do otherwise?

3 Replies


  • Hi Christophe,





    The variant you offered is incorrect. Here is an example demonstrating how to do this:







    Sub Main

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\Test\File.XML", 1)





    strText = objFile.ReadAll

    objFile.Close

    Log.Message(strText)

    Set RegExpr1 = HISUtils.RegExpr





    ' Note that the ^ and $ symbols have been removed

    RegExpr1.Expression = "<M_CODE>(.+)</M_CODE>"





    strNewText = RegExpr1.Replace(strText, "")

    Log.Message(strNewText)

    Set objFile = objFSO.OpenTextFile("C:\Test\File.XML", 2)

    objFile.WriteLine strNewText

    objFile.Close

    End Sub







    Does it help you?
  • I found
    the solution to my problem :



    Set objFSO = CreateObject("Scripting.fileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\Users\TestAdmin\Desktop\Essai.XML", 1)



    strText = objFile.ReadAll

    objFile.Close



    Set RegExM = New RegExp

    RegExM.Pattern = "<M_CODE>[\s\S]+</M_CODE>"



    RegExM.Global = True



    strNewTextM = RegExM.Replace(strText, "")

    Set objFile = objFSO.OpenTextFile("C:\Users\TestAdmin\Desktop\Essai.XML", 2)

    objFile.WriteLine strNewTextM

    objFile.Close