Mper

StringRegExpReplace Question

3 posts in this topic

Hello,

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")
Next

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



#2 ·  Posted (edited)

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)
Next
ConsoleWrite($NewText & @CRLF)

#cs ;Returns:-
,{....."Var1":true,"Var2":"HELLO1","Var1":true,"Var2":"TEST1","Var1":true,"Var2":"HELLO2","Var1":true,"Var2":"TEST2","...
#ce

 

Edited by Malkey
Added closing double quotes to Var2 value.
1 person likes this

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