Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Use regular expression to search and replace a string pattern

Former Member
0 Kudos

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




1 ACCEPTED SOLUTION

DoanManhQuynh
Active Contributor

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}]'.

9 REPLIES 9

former_member199637
Participant

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 '}],'.

NTeunckens
Active Contributor
0 Kudos

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 ...

DoanManhQuynh
Active Contributor

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}]'.

Former Member
0 Kudos

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

Former Member
0 Kudos

Hello,

can anyone help me please?

Best regards,

Christian

0 Kudos

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.

Former Member
0 Kudos

Hi quynh.doanmanh,

all I need is a regular expression, that is doing the following:

  1. Find all appearances of the following string pattern: "custom_variables":{" and replace it by "custom_variables":[{"
  2. Search for next, following } character and change it to }]

I cannot do this with a simple REPLACE.

Best regards,

Christian

0 Kudos
the regex i gave you did what you want but incase you want it to distinguish between custom_variable and other_variable then i think it a bit difficult because they all character with same format. this is just your sample so the regex may not exactly, you have to check it by yourself and there is also have a good F1 document on REGEX statement.

0 Kudos

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.