×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Pattern matching comments

Pattern matching comments

Pattern matching comments

(OP)
I am trying to use regular expression to delete comments using /* whatever */ in a file.

The code is as follows-

$fileContent;       #Content of file contains /* */ and
print "BEFORE\n" . $fileContent . "\n---------------\n";
$fileContent=~ s/\/\*(.*)\*\///g;
print "AFTER \n" . $fileContent;


The 'AFTER' print out is identical to the 'BEFORE' print!

$fileContent=~ s/(\/\*)(.*)(\*\/)//g;
gives same print out.

$fileContent=~ s/(\/*)(.*)(*\/)//g; ΓΏ
gives error /(/*)(.*)(*/)/: ?+*{} follows nothing in regexp at comment.pl line 4.


The problem is that * and \ are both escape character, I think.

RE: Pattern matching comments

(OP)
I tried s/\*[^*]*\*+([^/*][^*]*\*+)*/##g; from http://www.effectiveperl.com/EP.04.pdf, I used s/ instead of s# (don't know what s# is??) and  got error /\*[^*]*\*+([^/: unmatched [] in regexp at comment.pl line 4.

Hmm.......

RE: Pattern matching comments

(OP)
$fileContent=~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/##g; works, thought don't know what s# is!

RE: Pattern matching comments

Is this what you are trying to do?  If your comment spans across multiple lines, then you need the 's' switch on the end of the s///gs.  Otherwise, the regex stops at the first line ending it hits.


#!/usr/local/bin/perl

$fileContent = 'some text
/* a comment

here */
more text after comment';

$fileContent =~ s/\/\*.*\*\///gs;
print "$fileContent\n";


HTH


 
 
 keep the rudder amid ship and beware the odd typo

RE: Pattern matching comments

s# is the same as s/ except that it uses the # character as the delimiter, so you can use / inside the regular expression without having to escape it. If you use the s(somechar) form you can use any character you want as the delimiter. You can even use paired delimiters like:

s[...][...]
s(...)(...)
s{...}{...}
s<...><...>

Being able to use any character you want comes in very handy when your regular expressions contain slashes.

Note that this "generalized" for also works for matching (use m#...#) and translating (tr#...#...#). It even works for quoted strings (q[...] instead of "...")!

See the perlop manpage under "Quote and Quotelike Operators" for more details.

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close