On 4/25/05, John W. Krahn <[EMAIL PROTECTED]> wrote:
> Owen wrote:
> > I found a message from Randal Schwartz, Message-ID:
> > <[EMAIL PROTECTED]>#1/1
> > which gave a regular expression for a valid Unix name,
> >
> >       /^(?=.*?\D)[a-z\d]+$/
> >
> > That works but why does it work?
> >
> >     /
> >       ^               # Start of a string
> >        (?=            # 0 or 1 instance of
> >           .*?         # anything but a newline
> >               \D      # Non digit
> >        )              #
> >
> >       [a-z\d]+        # All match a-z and any digit at
> >               $       # End of a string
> >     /
> >
> >
> > I tried breaking it down like above but it still doesn't say "Must not be
> > all numbers and letters must be all lowercase"
> >
> > Any help in turning that re into plain words would be appreciated
> 
> (?=.*?\D) is a zero-width assertion so it doesn't affect the match which is
> /^[a-z\d]+$/ -- match a string whose only characters are a-z0-9.  When you add
> the zero-width assertion it ensures that there is at least one \D character in
> the match.
> 
> John

I think this may be part of the confusion, too:

> >        (?=            # 0 or 1 instance of
> >           .*?         # anything but a newline

/.*?/ is not that same as /.?/.  /.?/ matches zero or 1 of any
character but the newline.  /.*?/, however, matches zero or more, just
like /.*/.  It's non-greedy, though, so it will stop matching at the
first occurance of \D instead of the last.  In this case it boils down
roughly to /(?=[^\D]{0,}\D/.

HTH,

--jay

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to