For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > Regular expression help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regular expression help
Owen

2005-04-24, 8:55 pm


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


TIA



Owen
Charles K. Clarkson

2005-04-25, 3:56 am

Owen <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

John W. Krahn

2005-04-25, 3:56 am

Owen 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 add
the zero-width assertion it ensures that there is at least one \D character in
the match.


John
--
use Perl;
program
fulfillment
Jay Savage

2005-04-25, 3:56 pm

On 4/25/05, John W. Krahn <krahnj@telus.net> wrote:
> Owen wrote:
be[color=darkred]
>=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:
[color=darkred]

/.*?/ 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com