Jump to content

StringRegExp vs StringRegExpReplace


Recommended Posts

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?

Link to comment
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.

Edited by Artisan
Link to comment
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")
Link to comment
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!
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...