Previous

Next


06. Greediness (continued)

  • Lets look at another example of greediness, and how it can give a result you may not have been expecting.
  • Consider this sentance:
      This isn't fantastic, let's drink fanta!
    
  • Say we use Perl to match this with the regex:
      fanta(.*)
    
  • What do we expect to be in the variable $1 (i.e. in the brackets as matched by ".*")?
  • Well, we know that "." will match all characters (except those we talked about last lecture), so we would expect to see the following match:
      This isn't fantastic, let's drink fanta!
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    So, $1 would be:
      stic, let's drink fanta!
    
  • Now, what if we said the regex was:
      fanta(.*).*
    
  • Would anything change?

Previous

Next

Andrew Hill

For LinuxSA Meeting, 17 April 2001