Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Unnecessary use of capturing in regex?

by theroninwins (Friar)
on Nov 03, 2004 at 10:03 UTC ( #404851=note: print w/replies, xml ) Need Help??

in reply to Unnecessary use of capturing in regex?

I also thought that the 2 scripts are exactly the same but thought that I was missing somehting that's why i didn't write it but i guess I'm not the only one that sees it this way.
So why the post???
What is the problem??
the answer to your given question is because they are the same
  • Comment on Re: Unnecessary use of capturing in regex?

Replies are listed 'Best First'.
Re^2: Unnecessary use of capturing in regex?
by biofeng918 (Acolyte) on Nov 03, 2004 at 13:20 UTC

    I am so sorry that one of the codes just are the following:

    #!\usr\bin\perl -w for($_="head";s/(.)//;){ print"now head is $_.\n"; }
      ...tell me why the two can come to the same result...
      #!\usr\bin\perl -w for($_="head";s/(.)//;){ print"now head is $_.\n"; } #!\usr\bin\perl -w for($_="head";s/.//;){ print"now head is $_.\n"; }

      The only difference between your (updated) two samples, is that in the first you have capturing brackets. These will cause the matched character to be copied into $1. But then you do nothing with $1, so otherwise both samples are the same:

      Match a (the first) character in $_, and then delete it; print out the contents of $_; repeat until no match occurs, which will be when $_ is empty.

      What difference were you expecting?

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
        Thank you for your explainationS to me,I know the "()" has memory function, also I know that the function of ".", I enconter
        #!\usr\bin\perl -w for($_="head";s/(.)//;){ print"now head is $_.\n"; }
        in the book learning perl, I am confused why not just "." So I ask this question.

        Janitored by Arunbear - added code tags, as per Monastery guidelines

      They give the same output because they are still technically the same.

      The only difference is the parenthesis in the regex around the "any single character" ( or "." ).

      The role of parenthesis here is to group parts of the regex together and to create back references ($1, $2 etc). See the perlre documentation for full details on what it does.

      In this particular case it's making no difference to the operation of the regex in question.

      --- Jay

      All code is untested unless otherwise stated.
      All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
      If in doubt ask.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://404851]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2022-01-22 12:57 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (62 votes). Check out past polls.