hudsonhock Posted April 14, 2022 Share Posted April 14, 2022 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): 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. 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 More sharing options...
Subz Posted April 14, 2022 Share Posted April 14, 2022 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 hudsonhock 1 Link to comment Share on other sites More sharing options...
Nine Posted April 14, 2022 Share Posted April 14, 2022 (edited) @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 April 14, 2022 by Nine hudsonhock and rudi 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
hudsonhock Posted April 15, 2022 Author Share Posted April 15, 2022 @Subz I'll keep in mind on this, thanks. @Nine Oh boy, I thought they work the same way with just an additional replace function for StringRegExpReplace. Thanks for the working code. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now