Jump to content

Replacing Strings


Recommended Posts

Uh, hi, I'm back again... with another question:

I'm trying to open a list of .txt files and replace certain strings within them with one string. So, I understand I could use _ReplaceStringInFile, but is it possible to to replace two characters and everything inbetwen them, regardless of what the innards might be? For instance, I could be trying to replace "(jack.what)" with "pizza" in one file. But, in another file its not "(jack.what)" but "(john.what)." Is it possible for AutoIt to somehow recognize the strings resembling a pattern, so that it could replace them, without having to actually specify what it exactly has to replace?

Link to comment
Share on other sites

StringRegExpReplace()

As per your example:

$Test = "Blah Blah Blah this is sample text (john.what) more text (jack.what) even more text!!!! (OMFG DIFFERENT TEXT)"

$Result = StringRegExpReplace($Test, "\((.*?)\)", "pizza"); replaces anything in parenthesis with "pizza"

MsgBox(0,"","Before: " & @CRLF & $Test & @CRLF & @CRLF & "After: " & @CRLF & $Result)
Link to comment
Share on other sites

$handle=FileOpen("file.txt",0)
$text1=FileRead($handle)
$text2=StringRegExpReplace($text1,"\((.*?)\)","word")
_ReplaceStringInFile("path\file.txt",$text1,$text2"",0)

So, this would be how I would use that function to edit the file, right?

Note: Nvm about this note, if you already read it before this last edit. I understand it now. Thanks.

Edited by Jon8c
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...