perl expression to change not in group of value

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

perl expression to change not in group of value

Hello,

 

I want to change every character that not ST or LI to " "

I wrote this code:

 

data _null_;
a='ELISG_ST#AT_#S_STD#D="Y"';
b=prxchange('s/[^(ST|LI)]/ /',-1,a);
put a=;
put b=;
run;

 

 

a=ELISG_ST#AT_#S_STD#D="Y"

this is the output:
b=LIS ST T S ST

this is the desire output:

LI ST ST

 

how can I use not without this [] brackets that create class group?


Accepted Solutions
Solution
‎10-31-2017 02:54 AM
PROC Star
Posts: 1,935

Re: perl expression to change not in group of value

^ can only be used in a [] class.

But this works too if you want another way:


data _null_;
  a='ELISG_ST#AT_#S_STD#D="Y"';
  b=prxchange('s/.*?((ST)|(LI))?.*?/$1/',-1,a);
  put a= / b=;
run;

b=LISTST

 

View solution in original post


All Replies
Super User
Posts: 10,325

Re: perl expression to change not in group of value


data _null_;
a='ELISG_ST#AT_#S_STD#D="Y"';
length b $ 100;
do i=1 to length(a)-1;
 temp=substr(a,i,2);
 if temp  in ('ST' 'LI') then b=catx(' ',b,temp);
end;
put a=;
put b=;
run;
Occasional Contributor
Posts: 11

Re: perl expression to change not in group of value

Hi,
Thanks for the answer, but I am looking for a way to do it with regular
expressions
Super User
Posts: 10,325

Re: perl expression to change not in group of value

Opps.
Delete the OUTPUT statement.


Super User
Posts: 10,325

Re: perl expression to change not in group of value

OK. No problem.


data _null_;
a='ELISG_ST#AT_#S_STD#D="Y"';


length b $ 200;
start=1;
end=length(a);
pid=prxparse('/ST|LI/');
call prxnext(pid,start,end,a,p,l);
do while(p>0);
 b=catx(' ',b,substr(a,p,l));
 output;
 call prxnext(pid,start,end,a,p,l);
end;

put a=;
put b=;
run;


Occasional Contributor
Posts: 11

Re: perl expression to change not in group of value

Thanks for your help

 

I am doing this as part of learn regular expression, so I am not looking for a solution to this specific problem.

I try to understand how to do it only with regular expression.

 

if I want to change ST and LI

I will use the following code:

data _null_;
a='ELISG_ST#AT_#S_STD#D="Y"';
b=prxchange('s/(ST|LI)/ /',-1,a);
put a=;
put b=;
run;

I am looking for a way to put not on (ST|LI) 

any Ideas?

Solution
‎10-31-2017 02:54 AM
PROC Star
Posts: 1,935

Re: perl expression to change not in group of value

^ can only be used in a [] class.

But this works too if you want another way:


data _null_;
  a='ELISG_ST#AT_#S_STD#D="Y"';
  b=prxchange('s/.*?((ST)|(LI))?.*?/$1/',-1,a);
  put a= / b=;
run;

b=LISTST

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 73 views
  • 0 likes
  • 3 in conversation