Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Regular expression help
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

Report this thread to moderator Post Follow-up to this message
Old Post
Owen
04-25-05 01:55 AM


RE: Regular expression help
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


Report this thread to moderator Post Follow-up to this message
Old Post
Charles K. Clarkson
04-25-05 08:56 AM


Re: Regular expression help
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 a
dd
the zero-width assertion it ensures that there is at least one \D character 
in
the match.


John
--
use Perl;
program
fulfillment

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
04-25-05 08:56 AM


Re: Regular expression help
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Jay Savage
04-25-05 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:22 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.