Jump to content

Edit file with StringRegExpReplace


Recommended Posts

Hello everyone,

I am trying to edit file with StringRegExpReplace but was puzzled with the outcome. 

#include <File.au3>

_EditFileWithRegex('"AnsiLogPath" type="string" data=(.*)\/', '"TestFilePath"')

Func _EditFileWithRegex($sFindPattern, $sStrReplace)
    Local $hFile, $sText, $sReplaceText, $sFile = "C:\Temp\ConEmu\Data\settings\ConEmu.xml"
    $hFile = FileOpen($sFile, $FO_READ)
    $sReplaceText = StringRegExpReplace(FileRead($hFile), $sFindPattern, $sStrReplace)
    $hFile = FileOpen($sFile, $FO_OVERWRITE)
    FileWrite($hFile, $sReplaceText)
    FileClose($hFile)
EndFunc

I am intended to capture the path after as following (highlighted in Yellow):

image.png.69d9c8c88d293c896be1c4fa515caeb7.png
 
Here's my test pattern, '"AnsiLogPath" type="string" data=(.*)\/'
Tested with StringRegExp is able to capture the string as highlighted, but with StringRegExpReplace the whole line is being replace rather than the highlighted string as result.
image.png.592f748edcf6e80a9fa773e0968352eb.png
 
I am confusing of why the entire line is being replaced when it capture only the highlighted string?

ConEmu.xml

Link to comment
Share on other sites

You need to use FileClose after the first FileOpen (in read mode) or use something like:

#include <File.au3>

_EditFileWithRegex('"AnsiLogPath" type="string" data=(.*)\/', '"TestFilePath"')

Func _EditFileWithRegex($sFindPattern, $sStrReplace)
    Local $sFile = "C:\Temp\ConEmu\Data\settings\ConEmu.xml"
    Local $sReplaceText = StringRegExpReplace(FileRead($sFile), $sFindPattern, $sStrReplace)
    Local $hFile = FileOpen($sFile, $FO_OVERWRITE)
    FileWrite($hFile, $sReplaceText)
    FileClose($hFile)
EndFunc

 

Link to comment
Share on other sites

@hudsonhock

StringRegExpReplace acts differently from StringRegExp.  When you specify surrounding characters, they are also replaced, but only the captured part can be back-referenced.  So in order to make it work, you will need to repeat the surroundings in the replacement string.

For example :

FileWrite("NewConEmu.xml", StringRegExpReplace(FileRead("ConEmu.xml"), '(name="AnsiLogPath" type="string" data=)".*"', '$1"NewValueHere"'))

But you could also use the XMLDOM object, which give you more control over an xml file :

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load("ConEmu.xml")
Local $oNode = $oXML.SelectSingleNode("//value[@name='AnsiLogPath']")
ConsoleWrite($oNode.xml & @CRLF)
$oNode.setAttribute("data", "testpath")
ConsoleWrite($oNode.xml & @CRLF)
$oXml.save("NewConEmu.xml")

 

Edited by Nine
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...