Wrox Programmer Forums
|
BOOK: Beginning Regular Expressions
This is the forum to discuss the Wrox book Beginning Regular Expressions by Andrew Watt; ISBN: 9780764574894
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Regular Expressions section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2009, 03:22 AM
Registered User
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default regular expression-- needed help

i have this: ^(?!.*--)[A-Za-z\d-]+$
it accepts alphanumeric characters, optionally with single dash only and it does not accepts space.

now i need it to accept space for example:
ab34 12435, ab34- 12435 is valid.
ab34 12435, ab34- 12435 is invalid.

never mind the space before and end of the example.. i can trim that before i validate it to my problem regular expression

any help please
 
Old September 5th, 2009, 04:33 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I'm sorry, but I can't see *ANY* difference between you valid and invalid examples.

I even used "view==>>source" to see your post, in case the forum was "eating" some characters, and they are identical. Thus:
Code:
ab34 12435, ab34- 12435 is valid. <br />
ab34 12435, ab34- 12435 is invalid.<br />
Try again??
 
Old September 5th, 2009, 08:58 PM
Registered User
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it needs to accept alphanumeric with optionally a non-consecutive dash and optionally a non-consecutive space.

it accepts:
12a-3c-4f3fg
12ertgg2
1-2-3-3-4-3
dffgsfg
d-f-f-g-s-f-g

it does not accept:
12-3c-4f&3fg
12e%rt-gg2
d--f-f-g-s-f-g
1-2-3-3-4--3
 
Old September 6th, 2009, 03:51 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Wish you had given more *different* examples.

Non-consecutive dash or space, okay. But what about dash followed by space or vice versa?
And can the sequence begin or end with a dash or space??
Code:
a1 -b2
c3- d4
-e5-f6
g7-h8-
Are any/all of those valid???

Anyway...assuming that *NONE* of those are legal:
Code:
/^([a-z0-9]+[ \-])*[a-z0-9]+$/i
I think that does it. That's JavaScript regexp notation.

You see it?
Code:
/^ === start
([a-z0-9]+[ \-])* === zero or more of
     [a-z0-9]+  === one or more alphanums
     [ \-]  === followed by either space or dash
[a-z0-9]+  === ending with one or more alphanums
$  === end 
/i  === ignore case of alphas
 
Old September 6th, 2009, 09:39 PM
Registered User
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you sooooo much sir. i really appreciate it..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression Help Please rstelma ASP.NET 1.0 and 1.1 Professional 0 January 2nd, 2008 07:01 PM
regular expression if-then smay Javascript 4 July 5th, 2005 12:10 PM
Regular Expression Help anshul PHP How-To 4 December 8th, 2004 05:19 AM
Regular Expression Help Greg Griffiths Javascript How-To 4 November 12th, 2004 05:33 AM
help in regular expression diby Beginning PHP 1 September 17th, 2003 12:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.