DATA Step, Macro, Functions and more

Perl Regular Expression

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Perl Regular Expression

I am working on some RTF code generation. I am using following code that is working fine but i am not well in PRX functions.

Is there any way to simplyfy my code?

Code:

data _null_;
   length tmptxt $100.;
   tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic";
   tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
   tmptxt= prxchange('s/([\$][a-zA-Z]+)([\(])/%$1$2/',-1,tmptxt);
   tmptxt=prxchange('s/%\$/%/',-1,tmptxt);
   tmptxt=prxchange('s/([\$][a-zA-Z]+)/&$1$2/',-1,tmptxt);
   tmptxt=prxchange('s/&\$/&/',-1,tmptxt);
   put tmptxt;
run;

Thanks in advance


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Trusted Advisor
Posts: 1,301

Re: Perl Regular Expression

 

You can reduce the number of PRXCHANGE functions by collapsing the two-step changes you are making for $->% and $->&

 


data _null_; length tmptxt $100.; tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic"; tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt); /* tmptxt=prxchange('s/([\$][a-zA-Z]+)([\(])/%$1$2/',-1,tmptxt); tmptxt=prxchange('s/%\$/%/',-1,tmptxt); */ tmptxt=prxchange('s#[\$]([a-z]+)(?=\()#%$1#i',-1,tmptxt); /* tmptxt=prxchange('s/([\$][a-zA-Z]+)/&$1$2/',-1,tmptxt); tmptxt=prxchange('s/&\$/&/',-1,tmptxt); */ tmptxt=prxchange('s#[\$]([a-z]+)#&$1#i',-1,tmptxt); put tmptxt; run;

 

With comments removed:

 

data _null_;
   length tmptxt $100.;
   tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic";
   tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
   tmptxt=prxchange('s#[\$]([a-z]+)(?=\()#%$1#i',-1,tmptxt);
   tmptxt=prxchange('s#[\$]([a-z]+)#&$1#i',-1,tmptxt);
   put tmptxt;
run;

 

View solution in original post


All Replies
Solution
‎09-25-2015 06:23 AM
Trusted Advisor
Posts: 1,301

Re: Perl Regular Expression

 

You can reduce the number of PRXCHANGE functions by collapsing the two-step changes you are making for $->% and $->&

 


data _null_; length tmptxt $100.; tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic"; tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt); /* tmptxt=prxchange('s/([\$][a-zA-Z]+)([\(])/%$1$2/',-1,tmptxt); tmptxt=prxchange('s/%\$/%/',-1,tmptxt); */ tmptxt=prxchange('s#[\$]([a-z]+)(?=\()#%$1#i',-1,tmptxt); /* tmptxt=prxchange('s/([\$][a-zA-Z]+)/&$1$2/',-1,tmptxt); tmptxt=prxchange('s/&\$/&/',-1,tmptxt); */ tmptxt=prxchange('s#[\$]([a-z]+)#&$1#i',-1,tmptxt); put tmptxt; run;

 

With comments removed:

 

data _null_;
   length tmptxt $100.;
   tmptxt="a\b Refer $crossref(Table 1) and $italic.Section $crossref(3.1)$enditalic";
   tmptxt=prxchange('s/([\\{}])/\\$1/',-1,tmptxt);
   tmptxt=prxchange('s#[\$]([a-z]+)(?=\()#%$1#i',-1,tmptxt);
   tmptxt=prxchange('s#[\$]([a-z]+)#&$1#i',-1,tmptxt);
   put tmptxt;
run;

 

Contributor
Posts: 20

Re: Perl Regular Expression

Hi FriedEgg

 

Thanks a bunch.

 

I reduced my code Smiley Happy

 

Could you explain two things

  1. What is the role of ( )? becuase i misplaced in my code
  2. I know ? is zero or one time. what is the meaning of ?=

 

Thanks in advance.

Rajaram

 

Trusted Advisor
Posts: 1,301

Re: Perl Regular Expression

This is a non-capturing group that performs a positive look-ahead assertion.

 

[\$]([a-z]+)(?=\()

Options: case insensitive; ^ and $ match at line breaks

Match a $ character «[\$]»
Match the regular expression below and capture its match into backreference number 1 «([a-z]+)»
   Match a single character in the range between “a” and “z” «[a-z]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=\()»
   Match the character “(” literally «\(»
Trusted Advisor
Posts: 1,137

Re: Perl Regular Expression

I could help you to understand what the prxchange function is doing, as you are aware the prxchange function is used to replace the captured text and replace with another text or make it blank. Here are explanations of what the prxchange function is trying to do. prxchange is best for these kind of replacements.

 

prxchange('s/([\\{}])/\\$1/',-1,tmptxt); /* trying to locate any one of \\{} and replace with 
blank*/
prxchange('s/([\$][a-zA-Z]+)([\(])/%$1$2/',-1,tmptxt); /* Trying to locate to capture buffers
first capture for $ followed by any alphabet followed by + and second capture bufferfor \(
and replace them with %*/

 

prxchange('s/%\$/%/',-1,tmptxt); /*

 

prxchange('s/%\$/%/',-1,tmptxt);/* trying to locate % followed by $ and when found this will be 
replaced with %*/

 

prxchange('s/([\$][a-zA-Z]+)/&$1$2/',-1,tmptxt); /* Trying to locate a text with $ followed by 
any small or capital letter alphabet followed by + , then this whole text is when identified
will be replaced with &*/


prxchange
('s/&\$/&/',-1,tmptxt);
/* trying to locate the special character like & $ and
replace with &*/

 

Thanks,

Jag

 

Thanks,
Jag
🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 258 views
  • 0 likes
  • 3 in conversation