Regular Expression Newbie Question

Discussion in 'Perl Misc' started by ahjiang@gmail.com, Apr 12, 2006.

  1. Guest

    Hi all,

    I have a few simple question here

    $program = "/*Hello world*/ How are you";

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


    Output: How are you

    I understand im using the s/PATTERN/REPLACEMENT/egimosx this.

    1) Why does the above syntax still works. It doesnt follow s///.
    2) \* Match the opening delimiter. The opening delimiter in $program is
    /* how come \* still matches it?

    Appreciate any help.
    , Apr 12, 2006
    #1
    1. Advertising

  2. Guest

    wrote:

    >
    > 1) Why does the above syntax still works. It doesnt follow s///.
    > 2) \* Match the opening delimiter. The opening delimiter in $program is
    > /* how come \* still matches it?
    >
    > Appreciate any help.



    1) You can use almost any char as a delimiter in a substitution (or a
    match for that matter) - in most cases you will see s/// but you will
    sometimes see different ones being used

    >From perldoc perlrequick


    ----------------------------------------------------------------------------------------------------------------------
    # convert percentage to decimal
    $x = "A 39% hit rate";
    $x =~ s!(\d+)%!$1/100!e; # $x contains "A 0.39 hit rate"

    The last example shows that "s///" can use other delimiters, such
    as
    "s!!!" and "s{}{}", and even "s{}//". If single quotes are used
    "s'''",
    then the regex and replacement are treated as single quoted
    strings.
    ----------------------------------------------------------------------------------------------------------

    2) The '\' is escaping the first '*' since '*' is a special character
    in a regex. Therefore it is just looking for a match on '*' not '\*' .
    Similarly for the second '*'.

    Hope this helps
    , Apr 12, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,266
  2. Lee
    Replies:
    6
    Views:
    346
    Alan Moore
    Oct 14, 2003
  3. googleboy
    Replies:
    6
    Views:
    320
    Paul McGuire
    Sep 23, 2005
  4. Peter Vanderhaden
    Replies:
    5
    Views:
    123
    SpringFlowers AutumnMoon
    Sep 30, 2007
  5. Sven
    Replies:
    5
    Views:
    78
    Helgi Briem
    Oct 24, 2003
Loading...

Share This Page