1

We try to extract attributes using RegExMatch function from the strings with format of

"row=10#seat=15#barcode=9X4H341ZTKTPMintT#price=140.00"

to store them as individual variables. Unfortunately, I'm not very experienced with regular expressions syntax. Would really appreciate your help!

2
1

Not a pro in RegEx but i would go for something like

%%[
    var @s,@p_row, @p_seat, @p_barcode, @p_price
    var @row, @seat, @barcode, @price

    set @s = '"row=10#seat=15#barcode=9X4H341ZTKTPMintT#price=140.00"'

    set @p_row = '^.*row=(.*)#s'
    set @p_seat = '^.*seat=(.*)#b'
    set @p_barcode = '^.*barcode=(.*)#p'
    set @p_price = '^.*price=(.*)"'

    set @row = RegExMatch(@s, @p_row, 1)
    set @seat= RegExMatch(@s, @p_seat, 1)
    set @barcode= RegExMatch(@s, @p_barcode, 1)
    set @price= RegExMatch(@s, @p_price, 1)

]%%

Row:%%=v(@row)=%%<br>
Seat: %%=v(@seat)=%%<br>
Barcode: %%=v(@barcode)=%%<br>
Price: %%=v(@price)=%%<br>

The first one isn't working properly but this one does:

%%[
    var @s, @p_row, @p_seat, @p_barcode, @p_price
    var @row, @seat, @barcode, @price

    set @s = '"row=10#seat=15#barcode=9X4H341ZTKTPMintT#price=140.00"'
    set @p_row = '^.*row=(.*)#s'
    set @row = RegExMatch(@s, @p_row, 1)

    set @s = '"row=10#seat=15#barcode=9X4H341ZTKTPMintT#price=140.00"'
    set @p_seat = '^.*seat=(.*)#b'
    set @seat= RegExMatch(@s, @p_seat, 1)

    set @s = '"row=10#seat=15#barcode=9X4H341ZTKTPMintT#price=140.00"'
    set @p_barcode = '^.*barcode=(.*)#p'
    set @barcode= RegExMatch(@s, @p_barcode, 1)

    set @s = '"row=10#seat=15#barcode=9X4H341ZTKTPMintT#price=140.00"'
    set @p_price = '^.*price=(.*)"'
    set @price= RegExMatch(@s, @p_price, 1)
]%%

Row:%%=v(@row)=%%<br>
Seat: %%=v(@seat)=%%<br>
Barcode: %%=v(@barcode)=%%<br>
Price: %%=v(@price)=%%<br>

And possibly the best solution:

%%[
    var @s
    var @p_allvar
    var @row, @seat, @barcode, @price

    set @s = '"row=10#seat=15#barcode=9X4H341ZTKTPMintT#price=140.00"'
    set @p_allvar = '^.*row=(.*)#seat=(.*)#barcode=(.*)#price=(.*)"'
    set @row = RegExMatch(@s, @p_allvar, 1)
    set @seat = RegExMatch(@s, @p_allvar, 2)
    set @barcode = RegExMatch(@s, @p_allvar, 3)
    set @price = RegExMatch(@s, @p_allvar, 4)


]%%

Row:%%=v(@row)=%%<br>
Seat: %%=v(@seat)=%%<br>
Barcode: %%=v(@barcode)=%%<br>
Price: %%=v(@price)=%%<br>
1
  • Awesome job, Johannes! Thank you so much! If anyone encounters alike problem there is also a good resource to test your regular expressions - regex101.com – Artur Krotkov Sep 27 '18 at 7:33
1

A solution using SSJS (don't think it's the best though because using eval):

%%[
   VAR @attributes
   SET @attributes = "row=10#seat=15#barcode=9X4H341ZTKTPMintT#price=140.00"
]%%

<script runat=server>
     Platform.Load("Core","1");

    //Variable from AMPscript
    var attributes = Variable.GetValue("@attributes");

    var arr = attributes.split('#');
    for(var i = 0; i < arr.length; i++){
       var variableName = arr[i].split('=')[0];
       var variableValue = arr[i].split('=')[1];
       eval(variableName + " = " + "'" + variableValue + "'");
    }

//To get the variables back in AMPscript
  Variable.SetValue("@row",row);
  Variable.SetValue("@seat",seat);
  Variable.SetValue("@barcode",barcode);
  Variable.SetValue("@price",price);

</script>

<br>
<b>row:</b> %%=v(@row)=%%
<b>seat:</b> %%=v(@seat)=%%
<b>barcode:</b> %%=v(@barcode)=%%
<b>price:</b> %%=v(@price)=%%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.