Sign in to follow this  
Followers 0
Artisan

StringRegExp vs StringRegExpReplace

5 posts in this topic

I'm rather new to AutoIt, but I've picked up the language quickly. Or not.

Have a look at the following code:

$DATA = 'Global $INVENTORY  = "We got stuff"' & @CRLF & 'Global $VARIABLE = ""'

$INVENTORY = "Mine!"
$VARIABLE = "some value"

Dim $Test1 = StringRegExp($DATA, '\$INVENTORY[[:blank:]]*=[[:blank:]]*"(.*)"', 1)
Dim $Test2 = StringRegExp($DATA, '\$VARIABLE[[:blank:]]*=[[:blank:]]*"(.*)"', 1)
MsgBox(0, "StringRegExp", "'" & $Test1[0] & "'" & @CRLF & "'" & $Test2[0] & "'")

$DATA = StringRegExpReplace($DATA, '\$INVENTORY[[:blank:]]*=[[:blank:]]*"(.*)"', $INVENTORY)
$DATA = StringRegExpReplace($DATA, '\$VARIABLE[[:blank:]]*=[[:blank:]]*"(.*)"', $VARIABLE)

MsgBox(0, "StringRegExpReplace", $DATA)

Message boxes:

StringRegExp
--------------
'We got stuff'
''

StringRegExpReplace
-------------------
Global Mine!
Global some value

I'm trying to locate and update variables in the $DATA string (which in reality is read from another .au3 file). Using StringRegExp, it's correctly identifying the value of the variables. (That's just a test to ensure I'm not crazy.) When I use the exact same string for StringRegExpReplace, it's replacing the whole search string, rather than just the return group. What's going on? If need be, I can counter this by replacing the whole variable declaration, but it just seems wrong somehow. I'm using AutoIt v3.2.10.0 on Windows 2000, but the latest update shouldn't have anything to do with this issue. Help?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I don't think it's an AutoIt error.

I think I have to use '\1' or '\2' etc in my replace function somehow (instead of just $INVENTORY), but I can't make heads or tails of that as yet. I've searched the help files, and I'm looking at other examples, but it's not clicking yet.

Edited by Artisan

Share this post


Link to post
Share on other sites

I'm kind of confused about your goal. AutoIt doesn't process variables inside quotes. I can't tell if you are trying to match actual lines of code from a script or????

Share this post


Link to post
Share on other sites

I don't think it's an AutoIt error.

I think I have to use '\1' or '\2' etc in my replace function somehow (instead of just $INVENTORY), but I can't make heads or tails of that as yet. I've searched the help files, and I'm looking at other examples, but it's not clicking yet.

In order to use \1 and \2 you have to group them with parenthesis like so
$DATA = StringRegExpReplace($DATA, '(\$INVENTORY[[:blank:]]*=[[:blank:]]*")(.*)(")', "\1" & $INVENTORY & "\3")
$DATA = StringRegExpReplace($DATA, '(\$VARIABLE[[:blank:]]*=[[:blank:]]*")(.*)(")', "\1" & $VARIABLE & "\3")

Share this post


Link to post
Share on other sites

In order to use \1 and \2 you have to group them with parenthesis like so

$DATA = StringRegExpReplace($DATA, '(\$INVENTORY[[:blank:]]*=[[:blank:]]*")(.*)(")', "\1" & $INVENTORY & "\3")
$DATA = StringRegExpReplace($DATA, '(\$VARIABLE[[:blank:]]*=[[:blank:]]*")(.*)(")', "\1" & $VARIABLE & "\3")
Yeah, that's what I was after. I wasn't quite getting how the return groups worked. Now I do. Thanks a bunch!

Share this post


Link to post
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
Sign in to follow this  
Followers 0