Martin,
    Let me take a crack at it, and explain it so that it might help some
beginners, too.
I wrote this code:

$text='supernova';
if ($text=~/.n+..?.?v*./) {
  print "match.\n";
}

which just takes your regular expression and applies it. The if statement is
true, I ran it just to make sure.
Now, here's how I think it matches up, from left to right:
. matches one character, in this case, r
n+ matches one or more n, in this case, one n
. matches one character, in this case, o
.? matches 0 or 1 of any character, in this case, nothing
.? matches 0 or 1 of any character, in this case, nothing
v* matches 0 or more v, in this case, one v
. matches one character, in this case, a

To specifically answer your question, as long as I have everything correct
so far (someone correct me if I'm off base, please!), you do get a match,
because the regexp is not anchored to the beginning or end of the string,
and so a match starting in the middle of your string is fine and dandy. To
avoid that, you could do something like this:

$text='supernova';
if ($text=~/^.n+..?.?v*./) { # Note the ^ after the initial /
  print "match.\n";
}

The ^ now anchors to the beginning of the string, and the conditional fails.

In order to do the same at the end of the string, put a $ like this:
$text='supernova';
if ($text=~/.n+..?.?v*.$/) { # Note the $ before the last /
  print "match.\n";
}

Oh dear, I see many others have already replied. Well, there's one more
explaination, for whatever it's worth. By the way, I really like those other
explainations, it's informative to me as well, even though I've got a
correct solution. It just goes to show that while there is always more than
one way to DO it in Perl, there's also more than one way to EXPLAIN it.
Cool!

    Pete

Martin Weinless wrote:

> Hello,
>
> This is probably not a true beginners question - it refers to an example
> that I use in class when we discuss regexps.
>
> Here is the problem:
>
> take the regexp '.n+..?.?v*.'
>
> By all that is sacred, if we use the string 'supernova', there should be
> no match since there are too many characters before the 'n'
>
> However, any regexp checking code will report a match.
>
> Any ideas/comments/etc.
>
> Thanks
>
> Marty W.
>
> WINDOWS: 32 bit extensions and a graphical shell for a 16 bit patch to an
> 8 bit operating system originally coded for a 4 bit microprocessor,
> written by a 2 bit company that can't stand 1 bit of competition.

--
Mynd you, møøse bites Kan be pretty nasti...



Reply via email to