Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Another Expression question

Expand Messages
  • Paul Archer
    ... [snip] ... Look again at the script you wrote. It only prints if it matches. Remeber that it could match zero B s. So ccbba starts with zero B s. The
    Message 1 of 2 , Oct 27, 2005
    • 0 Attachment
      7:20pm, fkleinbu wrote:

      > Ok, so I go on reading in the book (O'Reilly camel book) and I see the
      > expression "/a|b*/ and the author asks how this would be evaluated..
      > Easy I think to myself, it will match any expression which contains
      > the letter "a" OR any number of the letter "b" including zero b's,
      > which basically will match anything.. So I write the following bit of
      > code to play with it a bit; also to test the special variables ($1,
      > $2… and $&, $`, and $') which are set when an expression is evaluated:
      >
      > #!/usr/bin/perl
      >
      > while (<STDIN>) {
      > if (/(a|b*)/) {
      > print "Line as entered = $_\n";
      > print "Match found (\$1) = $1\n";
      > print "Matched Part (\$&) = $&\n";
      > print "Before Match (\$`) = $`\n";
      > print "After Match (\$') = $'\n";
      > }
      > }
      >

      [snip]

      > Because the expression is
      > "b*" it matches zero or more "b's".. But then I enter "ccbba" and
      > get:
      >
      > Line as entered = ccbba
      >
      > Match found ($1) =
      > Matched Part ($&) =
      > Before Match ($`) =
      > After Match ($') = ccbba
      >
      > And this is where I am totally baffled.. The special variables tell
      > me nothing matched.. It should have matched since I didn't
      > specify and boundaries or qualifiers.. Also if it didn't match, it
      > should not have printed anything at all, since the printing is
      > controlled by the conditional of a match being made.. Any
      > ideas as to why this occurs?? Thanks in advance.. flk k
      >
      Look again at the script you wrote. It only prints if it matches.
      Remeber that it could match zero B's. So 'ccbba' starts with zero B's. The
      regexp matched, and $1 holds what it matched (you guessed it!) zero B's.


      Paul

      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.