I still have two question about the regexp used
1. What is "[]" at the end of the regexp
2. As you said perl lets us to chose any other thing other than slashes in
s///. But do we have to specify in particular what is the delimiter Or does
it take by default any charecter next to "=~ s"  as delimiter

Regards,
Amar


-----Original Message-----
From:   [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent:   Friday, April 20, 2001 5:39 PM
To:     [EMAIL PROTECTED]
Subject:        RE: Another regular expression question?



[EMAIL PROTECTED] said...
> I don't know how this works, But I have seen this regexp comparison it in
> perlop man pages. It has been very good regexp.
> Can anyone explain this for me.

I'll add some comments that may help explain some of what was left out:

#! /usr/bin/perl

# open the file
open(fileHandle, "Cfile") || die "can't open the file ";

# read the file in scalar
while(<fileHandle>) { # This is the Perl idiom for "while there
                      # are still lines left in the file..."

 $program .= $_;      # This adds the current line (including
                      # the newline at the end) to the variable
                      # called $program. At the end of this loop,
                      # $program contains the entire file living
                      # named 'Cfile'.
}

# Delete (most) C comments.
   $program =~ s {
   /\*     # Match the opening delimiter.
   .*?     # Match a minimal number of characters.
   \*/     # Match the closing delimiter.
} []gsx;

# This is a variation on the s///; operator, which replaces
# the thing between the first two slashes with the thing between
# the last two slashes. Perl lets you choose something other than
# a slash, if you want. This regex matches C comments, by looking
# for "/*" (the '*' is a wildcard, so you have to 'escape' it by
# putting a backslash in front of it) -- followed by as few characters
# as possible (.* means "anything, zero or more times, '?' means
# "do this only until the "next thing" comes up). The "next thing" in
# this regex is "*/" - the closing delimiter of C comments. Again, you
# have to escape the splat, so that Perl doesn't treat it specially.
#
# after the "[]", the 'g' means "match globally", that is, look
# for as many matches as you can.
# the 's' means treat the input as a single line. That is, don't
# stop looking for a match when you hit a newline.
# the 'x' means "let me have comments in my regex."

print $program



Reply via email to