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

New Free Tools on Dev Shed!

#1
July 16th, 2009, 08:58 AM
 gurjeet
Registered User

Join Date: Dec 2007
Posts: 29
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

#2
July 16th, 2009, 04:03 PM
 prometheuzz
User 165270

Join Date: Oct 2005
Posts: 497
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 937
This will do the trick:

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

#3
July 17th, 2009, 12:34 AM
 gurjeet
Registered User

Join Date: Dec 2007
Posts: 29
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..

#4
July 17th, 2009, 02:16 AM
 prometheuzz
User 165270

Join Date: Oct 2005
Posts: 497
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 937
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.

#5
July 17th, 2009, 02:35 AM
 gurjeet
Registered User

Join Date: Dec 2007
Posts: 29
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

#6
July 17th, 2009, 03:33 AM
 prometheuzz
User 165270

Join Date: Oct 2005
Posts: 497
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 937
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

 Viewing: Dev Shed Forums > Programming Languages - More > Regex Programming > Underscore or hiphen should come once but not at boundaries