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 July 16th, 2009, 07:58 AM
gurjeet gurjeet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 29 gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 9 h 26 m 42 sec
Reputation Power: 0
Underscore or hiphen should come once but not at boundaries

Hi

I am in a fix
i need to write a regular expression for this situation...

1) a-z and 0-9 allowed with _ or -
2) _ or - should not come more than one; and should not be at the boundaries and both cannot come together
3) range : 3 to 16

Examples::
1) te-st = true
2) test = true
2) te-_st = false
3) -test or test_ = false

This is what i came up with so far
Code:
^([a-z0-9]+(-|_)?[a-z0-9]+){3,16}$


Thank you for any help

Reply With Quote
  #2  
Old July 16th, 2009, 03:03 PM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
This will do the trick:

Code:
^(?!.*[_-]{2})[a-z0-9][a-z0-9_-]{1,14}[a-z0-9]$

Reply With Quote
  #3  
Old July 16th, 2009, 11:34 PM
gurjeet gurjeet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 29 gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 9 h 26 m 42 sec
Reputation Power: 0
Quote:
Originally Posted by prometheuzz
This will do the trick:

Code:
^(?!.*[_-]{2})[a-z0-9][a-z0-9_-]{1,14}[a-z0-9]$


Thank you
It solved the problem.

Just wondering how can we modify this expression to eliminate the double occurence of symbols

i.e. currently it is validating t-e_s and t-e-s but i want to allow people to use any of the symbol (_ or -) only once.
expression should not validate t-e_s and t-e-s..

Reply With Quote
  #4  
Old July 17th, 2009, 01:16 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Replace

Code:
(?!.*[_-]{2})


with

Code:
(?=[^_-]*[_-][^_-]*$)


Note that the string must n ow contain either a hyphen or an underscore. If it should be optional, you should make the [_-] class reluctant.

Reply With Quote
  #5  
Old July 17th, 2009, 01:35 AM
gurjeet gurjeet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 29 gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level)gurjeet User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 9 h 26 m 42 sec
Reputation Power: 0
Quote:
Originally Posted by prometheuzz
Replace

Code:
(?!.*[_-]{2})


with

Code:
(?=[^_-]*[_-][^_-]*$)


Note that the string must n ow contain either a hyphen or an underscore. If it should be optional, you should make the [_-] class reluctant.


wow! thanks
This is enough to make me feel like to seriously study regex. we cannot everytime copy and paste things

please if you can explain the first portion i.e. (?!.*[_-]{2}) ... and point me towards a gud regexp resouce

Reply With Quote
  #6  
Old July 17th, 2009, 02:33 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
You're welcome.

Quote:
Originally Posted by gurjeet
...

please if you can explain the first portion i.e. (?!.*[_-]{2}) ...


(?!...) is called "negative look ahead". So, when you do a(?!b), you will only match an 'a' when there's NOT a 'b' ahead of it.

Quote:
Originally Posted by gurjeet
... and point me towards a gud regexp resouce


IMO, the best online resource for regex is this one:
http://www.regular-expressions.info/tutorial.html

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Underscore or hiphen should come once but not at boundaries

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