Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: Greedy modifier found to be working non-greedy in a named group

by LanX (Archbishop)
on Nov 29, 2019 at 12:51 UTC ( #11109433=note: print w/replies, xml ) Need Help??


in reply to Re^2: Greedy modifier found to be working non-greedy in a named group
in thread Greedy modifier found to be working non-greedy in a named group

> both e* and e+ should have given same output. Why is the difference in output?

Good question, I think many are confused.

It's important to understand that empty matches exist and that e* means e{0}|e+ ( or something like e{0,32766} ).

So you are actually matching e{0} before all!

Printing out the match position of a capture group via @+ helps demonstrating it

DB<1> $_ = "This is a teeeext for testting"; DB<2> ;/(?<char>e*)/ and print "'$+{char}' is matched pattern at p +os $+[0]\n"; '' is matched pattern at pos 0 DB<3> ;/(?<char>e{0})/ and print "'$+{char}' is matched pattern at + pos $+[0]\n"; '' is matched pattern at pos 0 DB<4> ;/(?<char>e+)/ and print "'$+{char}' is matched pattern at p +os $+[0]\n"; 'eeee' is matched pattern at pos 15 DB<5> ;/(?<char>e{1,32766})/ and print "'$+{char}' is matched patt +ern at pos $+[0]\n"; 'eeee' is matched pattern at pos 15 DB<6>

update
  • Actually $+[0] gives you the end of the first match, $-[0] will give you the start.
  • e* is not limited in my Perl version but the upper bound in e{,} is.
  • couldn't find positions of named groups

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

) yes there is an maximal upper bound in range quantifiers, which I expected to be around 2**16, so it's an incomplete analogy

Replies are listed 'Best First'.
Re^4: Greedy modifier found to be working non-greedy in a named group
by rsFalse (Friar) on Nov 30, 2019 at 15:05 UTC
    > ...and that e* means e{0}|e+ ( or something like e{0,32766} ).

    > So you are actually matching e{0} before all!


    And if the sentence begins with "e.."? :)
      Good catch!

      The order must be reversed to reflect the greed.

      DB<8> x 'eeeA1234eB' =~ /(e+|e{0})(.)/ 0 'eee' 1 'A' DB<9> x 'A1234eB' =~ /(e+|e{0})(.)/ 0 '' 1 'A' DB<10> x 'A1234eB' =~ /(e+)(.)/ 0 'e' 1 'B' DB<11>

      Thanks!

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Updates

      Improved demo code

Re^4: Greedy modifier found to be working non-greedy in a named group
by rkabhi (Acolyte) on Nov 29, 2019 at 12:59 UTC
    That was very good explanation.

    Thanks a lot !!

    Regards,
    Abhishek

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2019-12-05 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (153 votes). Check out past polls.

    Notices?