Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 15th, 2009, 03:05 PM
4d4m 4d4m is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 2 4d4m User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 29 sec
Reputation Power: 0
Regex help required urgently

I have been trying to create a regex code to validate a string field in a form. below is what is required for the regex code:

The length of the input is bigger than 0 and less/equal to 14;
Only letters a to z (lower case), "-" (dash) and " " (space) are allowed,
The "-" (dash) and " " (space) letters must be entered,
The "-" or the " " letter must not be either the first or the last letter entered,
"-" must not be the immediate neighbour or adjacent (before or after) to a " ",
"-" or " " must not be the immediate neighbour (adjacent) of itself.

if anyone can help me create the code i would be very grateful.

cheers 4d4m

Reply With Quote
  #2  
Old January 15th, 2009, 07:14 PM
requinix's Avatar
requinix requinix is online now
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,877 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 8 h 8 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Whee! Help to know the language and regex flavor you're dealing with though. Perl?
Code:
/^(?=.{1,14}$)[a-z]+(-[a-z]+ | [a-z]+-)([a-z]+[- ])*[a-z]+$/i
  • ^: Starting at the beginning of the string, this:
  • (?=.{1,14}$): (ensures that there's 1 to 14 characters and then the end of the string*)
  • [a-z]+: checks that there is at least one letter
  • (-[a-z]+ : then a hyphen, letters, and a space
  • | [a-z]+-): or a space, letters, and a hyphen
  • ([a-z]+[- ])*: then some letters and a hyphen or space (repeated any number of times, or not at all)
  • [a-z]+$: and finally at least one more letter before the end of the string
/i means case-insensitive.

* Honestly, I'd remove this part and use whatever your language offers to check string length. Like a .length property, strlen function, len() method...

Tested against
Code:
abcdefghijklmn
abcd fghij-lmn
a-cd fghij-lmn
a-cd -ghij-lmn
a-cd fghij-lm-

Reply With Quote
  #3  
Old January 16th, 2009, 05:11 AM
4d4m 4d4m is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 2 4d4m User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 29 sec
Reputation Power: 0
I am going to be using the regex within Javascript to validate a name field on an online form.

cheers

4d4m

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Regex help required urgently

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap