codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Perl`s Greedy and Non-Greedy Quantifiers  iDLE at 10:00 on Thursday, April 26, 2007
 

hi,,,im a new comer from perl. actually this is my first server-side scripting to deal with. i like perl because it is difficult for some begginers... i started learning perl by april 1 this year 2007.

my delimma goes this way: there is a string "fred and barney are good friends". if i want to match "fred" and "barney the pattern will be (/fred.+barney/). but then using this kind of pattern may take up some time because the pattern is using the greedy quantifier. so to compromise the speed factor involve, we will use this pattern (/fred.+?barney/).

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

NOTE: "fred and barney are good friends" <-- the string

1. the fred word from the pattern will match to the string.
2. the (.+) greedy quantifier will match the character after the word "fred" which is ' ', then 'a', until 's', the last character of the string.
3. the barney word from the pattern will try to match the character after the word fred from the string. when the character won't match, it will go to the next character saving the last character and the current character which is 'a'. then the two character will be matched (" a") to the word barney. and if still can't match the process will repeat until the character saved become (" and barney"), then the word barney will now match. finally after matching with the word barney, the pattern match will exit.

ahm guys is this logic of greedy quantifier right? or if not then what's the right thing?

another thing is about the non-greedy quantifier. based on my own understanding from the "Llama book", the pattern (/fred.+?barney/) goes the logic this way:

NOTE: "fred and barney are good friends" <-- the string

1. the fred word from the pattern will match to the string.
2. the (.+?) greedy quantifier will match the character after the word "fred" which is ' ', then exits the pattern match.
3. the barney word from the pattern will try to match the character after the word fred from the string. when the character won't match, it will go to the next character saving the last character and the current character which is 'a'. then the two character will be matched (" a") to the word barney. and if still can't match the process will repeat until the character saved become (" and barney"), then the word barney will now match. finally after matching with the word barney, the pattern match will exit.

am i right about my logic of non-greedy quantifier? or if not then what's the right thing?

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

From: iDLE









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Please help me in putting javascript validation for dynamic radio button
•  Passing Records in TTX FILE
•  Drop-Down Menu problems !
•  Re: batch image croping
•  im stuck : (
•  im a noob.. pls help! (merge sort)
•  Re: help me how to execute a linux shell script file(.sh file ) in perl
•  Re: Help with Java Program!!
•  Re: positioning ContentPlaceHolder


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007