Part 1

the [] are just enclosing paired delimiters .. the substitution operator is
commonly used like this

  s/foo/bar/

but those '/' delimiters are just convention .. it can also be

  s#foo#bar#

or in fact any matching characters (that don't appear unescaped within the
regex or substitution)

  s qfooqbarq

bracketing constructs are treated a little differently .. in that instead of
matching the same character - they match the enclosing pair .. eg.

  s(foo)(bar)

and those can be nested .. eg.

  s(foo(foo)foo)(bar)

this will turn this string

  "blah foo(foo)foo blah"

into this string

  "blah bar blah"

you usually use the same brackets for the regex part of the substitution as
for the replacement part .. but you don't have to .. you can mix brackets

  s(foo){bar}

or as bbking wrote

  s{foo}[bar]

Part 2

perl parses the operator s/// whenever it sees it in context .. so the
following will make a substitution

  $_ = 'blah foo blah';

  s qfooqbarq;

but the following will not

  $_ = 'blah foo blah';

  @x = qw/s qfooqbarq/;

as with any piece of Perl code - it depends on the context as to what perl
does with it

-- 
  jason king

  In Hibbing, Minnesota, it shall be the duty of all policemen to kill
  all cats running at large. - http://dumblaws.com/


>-----Original Message-----
>From: Amarnath Honnavalli Anantharamaiah
>[mailto:[EMAIL PROTECTED]]
>Sent: Tue 24 Apr 2001 21:06
>To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
>Subject: RE: Another regular expression question?
>
>
>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