Jump to content

StringRegExpReplace Question

Recommended Posts



i'm currently struggling to simplify a program which i write (with StringRegExpReplace). 

Global $Variable[4] = ["HELLO1","TEST1","HELLO2","TEST2"]

$NewText = ',{....."Var1":true,"Var2":"1244324","Var1":true,"Var2":"677324","Var1":true,"Var2":"62364","Var1":true,"Var2":"235624","...'

For $i = 0 to 3
    $NewText = StringRegExpReplace($NewText, '(.*"Var2":").*(",".*)', "${1}" & $Variable[$i] & "${2}","4")

The idea is to Replace every field between        "Var2":      and      ","          with a new/next Value from $Variable.

My problem is that it only replaces the last Var2 with the last $Variable[].
All what i know about RegExp is from SEuBo 's Tutorial on several forums.

I'm trying to get better i can't get this to work. Guess I'm somehow blind in this case.

I really would appreciate help!

mfg Mper


Share this post

Link to post
Share on other sites

This could be what you are after. 

If you put the ConsoleWrite within the For - Next loop, you'll see what is being replaced on each loop as the count parameter reduces.

Global $Variable[4] = ["HELLO1", "TEST1", "HELLO2", "TEST2"]

$NewText = ',{....."Var1":true,"Var2":"1244324","Var1":true,"Var2":"677324","Var1":true,"Var2":"62364","Var1":true,"Var2":"235624","...'

For $i = 3 To 0 Step -1
    $NewText = StringRegExpReplace($NewText, '("Var2":")([^,]*)', "${1}" & $Variable[$i] & '"', $i + 1)
ConsoleWrite($NewText & @CRLF)

#cs ;Returns:-


Edited by Malkey
Added closing double quotes to Var2 value.
  • Like 1

Share this post

Link to post
Share on other sites

Thank you very much Malkey!

this works exactly how i wanted it :)

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


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.