hi,,,im a beginner on perl.actually it is my first server-side scripting language to deal with...for the reason that it is difficult for beginners.they say it is more on symbols unlike php with so many pre-defined routines, right?...but then i like to deal with the hard ones like assembly...

presently im reading the "Llama book".i started learning perl since april 1 this year.perl is great, and i think my summer is not a waste and im having a good time with it...except for one thing that i want to clarify about regular expression if you permit me.

my delima goes this way: there's a string like this "fred and barney went bowling last night", and if i want to match the words "fred" and "barney" my pattern goes this way (/fred.+barney/), right? but this pattern will take some time because i am using the greedy quantifier which is (.+), right? so to reduce the time it take to match like that one, i will use the non-greedy quantifier which is (.+?)...now my pattern will now look like this (/fred.+?barney/).

based on the book and from my own understanding the logic of the pattern (/fred.+barney/) goes this way:

  1. the fred word from the pattern will match to the string.
  2. the (.+) from the pattern will match the character next to the word fred, then the character 'a','n','d',' ', until it reaches 't' at the end of the string.
  3. then the word barney will be matched and if can't (.+) will match and that will be repeated until it reaches the character 'y', then the word barney will match again and because the word barney was present on the past characters that was tried to match (" and barney" <-- the characters that were matched lately), the word barney will now finally match, then the search for the pattern will now exit.

was my logic on pattern match using the greedy quantifier (.+) correct? if not, then what would be the correct way to understand perl's regular expression engine on that certain pattern?

my second question is about non-greedy quantifier (.+?)...based on the book and from my own understanding the logic of the pattern (/fred.+?barney/) goes this way:

  1. the fred word from the pattern will match to the string.
  2. the (.+?) from the pattern will match the character next to the word fred.
  3. the word barney will try to match and if can't (.+?) will match and that will be repeated until it reaches the character 'y', then the word barney will match again and because the word barney was present on the past characters that was tried to match (" and barney" <-- the characters that were matched lately), the word barney will now finally match, then the search for the pattern will now exit.

was it correct about my logic on non-greedy (.+?) quantifier? if not, then what would be the correct way to understand perl's regular expression engine on that certain pattern?

oww that's it...those are my two problems that needs answering...thanks in advance guys...keep deep and dark!

From: PerlPhi


In reply to About Greedy and Non-Greedy Regular Expressions by PerlPhi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.