10-24-2018 9:19 PM
Hi,
I hope someone can help me.
I want to build a regular expression (using REPLACE ALL OCCURRENCES OF REGEX ...) to modify a string (pattern) in a big string.
I have a big string (json) where I have a lot of string patterns that look like this one:
"custom_variables":{"product":"test","category":"washing_machine","sales_unit":"dach","ticket":"12345"},
"custom_variables":{"product":"test2","category":"washing_machine","sales_unit":"us","ticket":"9191191"},
They always start with "custom_variables":{ and end with }. I need to modify all occurrences of this pattern to look like this (just need to add some square brackets):
"custom_variables":[{"product":"test","category":"washing_machine","sales_unit":"dach","ticket":"12345"}],
Can anyone help me please building a working regular expression I can use?
Best regards,
Christian
10-25-2018 5:35 AM
i think it need to be replace 2 times:
first for the {:
DATA(reg1) = '(\W+\w+\W+)(\{)'.
REPLACE ALL OCCURRENCES OF REGEX reg1 IN str WITH '$1[{$`'.
second for the }:
DATA(reg2) = '("\w+":"\w+"|"\w+":"\d+")(\})'.
REPLACE ALL OCCURRENCES OF REGEX reg2 IN str WITH '$1}]'.
10-24-2018 10:49 PM
Hello Christian,
It seems that you will not require REGEX. Try code piece below
Regards,
Puneet Desai
REPLACE ALL OCCURRENCES OF '"custom_variables":{' IN source_str WITH '"custom_variables":[{'.
REPLACE ALL OCCURRENCES OF '},' IN source_str WITH '}],'.
10-24-2018 11:10 PM
I share the thoughts of Puneet : why make things tedious when a simple REPLACE can do the trick?
You could always got through the "DEMO_REGEX_TOY" program and test some RegEx-expressions however ...
10-25-2018 5:35 AM
i think it need to be replace 2 times:
first for the {:
DATA(reg1) = '(\W+\w+\W+)(\{)'.
REPLACE ALL OCCURRENCES OF REGEX reg1 IN str WITH '$1[{$`'.
second for the }:
DATA(reg2) = '("\w+":"\w+"|"\w+":"\d+")(\})'.
REPLACE ALL OCCURRENCES OF REGEX reg2 IN str WITH '$1}]'.
10-25-2018 7:39 AM
Hi all,
thank you very much for your help and the examples.
I think a simple REPLACE will not really work. The closing brackets "}" occur multiple times in my JSON string structure. But I need to add square brackets only if there is a starting string like '"custom_variables":{'.
Maybe this was not clear enough in my first post!
I give you an example. This is how my big string looks like:
...
"other_variable":{"val":"string"},
"custom_variables":{"product":"test2","category":"washing_machine","sales_unit":"us","ticket":"9191191"},
"other_variable2":{"val2":"string2"},
...
"custom_variables":{"product":"test2","category":"washing_machine","sales_unit":"us","ticket":"9191191"},
I only need to add suqare brackets if there ist a starting structure with "custom_variables..." etc.. This may occur multiple times in my big string.
The result should look like this:
...
"other_variable":{"val":"string"},
"custom_variables":[{"product":"test2","category":"washing_machine","sales_unit":"us","ticket":"9191191"}],
"other_variable2":{"val2":"string2"},
...
"custom_variables":[{"product":"test2","category":"washing_machine","sales_unit":"us","ticket":"9191191"}],
So I think I need a regular expression for this!? Right?
Best regards,
Christian
10-26-2018 7:31 AM
10-26-2018 7:43 AM
so we need to know the difference between custom_variable and other_variable. if you already know what variable need to be replaced then you dont need REGEX function, just replace it.
10-26-2018 2:38 PM
Hi quynh.doanmanh,
all I need is a regular expression, that is doing the following:
I cannot do this with a simple REPLACE.
Best regards,
Christian
10-29-2018 12:06 AM
10-29-2018 7:14 AM
I think you might have to do the steps exactly as you stated possibly in a loop at use the FIND statement to find the offset of your Customs_variabels part and its corresponding closing } and use string expressions to get your wanted results.