Multiple search and replace within a string throug

2019-08-30 10:49发布

问题:

I have following macro variable:

%let text = abc\pqr{work};

I need to replace \ with \\, { with \{ and } with \} by regular expression.

The code I tried gives error.

%let text = abc\pqr{work};
data _null_;
   var=prxchange('s/\\/\\\\/|s/\{/ \\\{/|s/\}/ \\\}/',-1,"&text");
   put var;
run;

Also, if the text is "BOLD\ITALIC\ITALICBOLD\BOLDITALIC\B\I\IB\BI", I need to convert it to "\b\i\ib\bi\b\i\ib\bi".

Apologies for not giving the code I tried initially.

Thanks in advance.

回答1:

For your first problem, you should be separating each pattern into a separate PRXCHANGE. I don't think PRXCHANGE allows OR operand in it. If run this code, it will work for you:

%let text = abc\pqr{work};
data _null_;
   var=prxchange("s/\\/\\\\/",-1,"&text");
   var=prxchange("s/\{/\\\{/",-1,var);
   var=prxchange("s/\}/\\\}/",-1,var);
   put var;
run;

The same principle applies to your second query:

%let text = BOLD\ITALIC\ITALICBOLD\BOLDITALIC\B\I\IB\BI;
data _null_;
   var=prxchange("s/BOLD/b/",-1,"&text");
   var=prxchange("s/ITALIC/i/",-1,var);
   var=lowcase(var);
   put var;
run;

Regads, Vasilij



标签: regex sas