DATA Step, Macro, Functions and more

Perl regular expression for matching comments

Reply
Super Contributor
Posts: 256

Perl regular expression for matching comments

Hello,

 

I would like to know the regular expression I can use to match comments please? Such as /* This is a comment */

I would like to replace the comment with just this text "/* */" in order to save space within my variable.

 

Below is the code that I've been trying to use, just to see if I can match the opening of the comments tag, but I'm not sure how to do it.

 

without_comments = prxchange("s//\*\w+)/ /", -1, with_comments);

You're help will be greatly appreciated.

 

Thank you!

 

Kriss

Super User
Super User
Posts: 7,942

Re: Perl regular expression for matching comments

Sorry, what do you mean by:

"I would like to replace the comment with just this text "/* */" in order to save space within my variable."

Comments are not in a variable, they are in code.  Are you storing code in a dataset or something, why?

As for replacing it, comment could be anything.  Comes down to code parsing which isn't a simple matter.  // is also a comment, /* could go over mutiple line.  Various other things to bear in mind also.  

Super Contributor
Posts: 256

Re: Perl regular expression for matching comments

Hello RW9. I'm calculating CTC grades for laboratory results, and so the algorithms along with the comments are within my variables. I'm using this alongside call execute. 

 

I've seen a potential solution on this site, which goes over the things you have mentioned, i.e comments on multiple lines. I'm just trying to incorportate this into SAS.

 

http://blog.ostermiller.org/find-comment

 

Many thanks,

Super Contributor
Posts: 256

Re: Perl regular expression for matching comments

I've seen that this code can be used to match comments. Which is similar to what that website had. I just had to escape the forward slashes too. For my example, I can just use this simple case below. Because it does find the majority of my comments (if not all). I think when the results are in the dataset there may not be the issue of comments being on multiple lines.

 

pattern = prxparse("/\/\*.*\*\//");

 

Thanks for you help.

Ask a Question
Discussion stats
  • 3 replies
  • 143 views
  • 0 likes
  • 2 in conversation