http://www.perlmonks.org?node_id=575533

prasadbabu has asked for the wisdom of the Perl Monks concerning the following question:

Monks, today i was preparing question for beginners in regex. So to tell them, that we can use any delimiters for regex as per the documentation perlre, i chose the below question.

$str = 'abc.'; $str =~ s.\...; print $str;

Very well I know that its not good using the delimiters like '.' etc. Before taking the question i tested this and found the odd answer. I expected 'abc' but i got 'bc.'. Where am i going wrong? I came with this question in CB and i was sent here. :-)

Thanks in advance

Prasad

Replies are listed 'Best First'.
Re: Regex - Delimiter question
by Hofmator (Curate) on Sep 29, 2006 at 14:30 UTC
    See perlop under 'Gory details of parsing quoted constructs' -> 'Removal of backslashes before delimiters'

    With the '.' being the delimiter, the backslash before the dot is removed before the regex parser sees the expression. So your code is equivalent to s/.// which explains your result.

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Regex - Delimiter question
by cephas (Pilgrim) on Sep 29, 2006 at 14:26 UTC
    Your escaped dot is telling it to not act as a delimiter, not removing the match anythingness of the dot.
    $ perl -MO=Deparse -e 's.\...' s/.//; -e syntax OK
Re: Regex - Delimiter question
by GrandFather (Saint) on Sep 29, 2006 at 18:46 UTC

    I like the question and it has been well answered. However is it appropriate to pose "This is how you shouldn't do it" puzzles for beginners?

    It is much easier to figure out how to paint an elephant by seeing a good painting of an elephant than by seeing a whole pile of paintings of shadows of different parts of an elephant.

    Show your beginners good code and good technique. Keep them in the middle of the stream where the water is smooth and easy to negotiate. Save the rapids for later on when they can handle things a little.

    It is more cool to answer questions clearly than it is to try and be an obscure guru. A guru who can answer the most obscure questions in a clear fashion posesses much more fu than one who draws cryptic shadow drawings in the sand.

    If you need some help or would like your questions checked out by others, it wouldn't hurt at all to post a meditation as an RFC (request for comment). If it is a good set of questions (and by the time the monks have pored over it, it will be), it may even enter into the tutorial materials as a resource for others to use.


    DWIM is Perl's answer to Gödel
      I agree 100%. Well maybe 99%.

      I'm sure most of of us remember being taught "we can use any delimiters for regex......but don't."
      My first question was always "Why??" and this serves as a great example of why. It serves to show that unless you understand what you are doing you may get unexpected results that you will also not understand.

      So I agree that it's not good to show how not to do it, but it is sometimes helpful to show why you should not be surprised by some of the results you may find when you step outside of the saftey of more "standard" coding techniques.
      My 2 cents worth anyway.
Re: Regex - Delimiter question
by Khen1950fx (Canon) on Sep 30, 2006 at 02:36 UTC
    There's a slide-presentation for beginners on the web called "Regular Expression Mastery" by M.J. Dominus. You might find some of the slides useful for your beginners. It's simple and no nonsense. See:

    Regular Expression Mastery

Re: Regex - Delimiter question
by wfsp (Abbot) on Sep 29, 2006 at 14:42 UTC
    s.\Q\...
Re: Regex - Delimiter question
by Solo (Deacon) on Sep 29, 2006 at 14:39 UTC
    You're expecting greedy matching, perhaps?

    I see I've misread the problem...

    --Solo

    --
    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.