#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    29
    Rep 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. #2
  3. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    This will do the trick:

    Code:
    ^(?!.*[_-]{2})[a-z0-9][a-z0-9_-]{1,14}[a-z0-9]$
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    29
    Rep Power
    0
    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..
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    29
    Rep Power
    0
    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
  10. #6
  11. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    You're welcome.

    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.

    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

IMN logo majestic logo threadwatch logo seochat tools logo