Code Comments
Programming Forum and web based access to our favorite programming groups.I found a message from Randal Schwartz, Message-ID: <8cvi6k3cs5.fsf@gadget.c scaper.com>#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 al l numbers and letters must be all lowercase" Any help in turning that re into plain words would be appreciated TIA Owen
Post Follow-up to this messageOwen <mailto:rcook@pcug.org.au> wrote: : I found a message from Randal Schwartz, Message-ID: : <8cvi6k3cs5.fsf@gadget.cscaper.com>#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 (?= # Zero-width positive look ahead assertion. : .*? # anything but a newline : ) # : : [a-z\d]+ # All match a-z and any digit at [a-z\d]+ # Match a-z (lowercase only) and any digit : $ # 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 AFAIK, a (?= ... ) construct affects the contents of $&, $`, and $'. Can you show any code immediately following this regular expression and the whole line with it? HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328
Post Follow-up to this messageOwen wrote: > I found a message from Randal Schwartz, Message-ID: > <8cvi6k3cs5.fsf@gadget.cscaper.com>#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 a dd the zero-width assertion it ensures that there is at least one \D character in the match. John -- use Perl; program fulfillment
Post Follow-up to this messageOn 4/25/05, John W. Krahn <krahnj@telus.net> wrote:
> Owen wrote:
be
>=20
> (?=3D.*?\D) is a zero-width assertion so it doesn't affect the match whic=
h is
> /^[a-z\d]+$/ -- match a string whose only characters are a-z0-9. When yo=
u add
> the zero-width assertion it ensures that there is at least one \D charact=
er in
> the match.
>=20
> John
I think this may be part of the confusion, too:
/.*?/ 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 /(?=3D[^\D]{0,}\D/.
HTH,
--jay
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.