Page 1 of 1
String Pattern
Posted: Fri Mar 13, 2009 10:10 am
by vayumahesh
I need a regular expression to validate a string with the following requirements.
1. May contain Upper case, Lower case alphabets, Numbers and hyphens ( this works - "^[a-zA-Z0-9-]*$" )
Not sure how to handle the second requirement
2. The hyphen should not be at the beginning or end of the string. Also, no more than one hyphen consecutively.
Example Strings
12Ab3, Ba3, aBc-32, a1-b2-c3
Re: String Pattern
Posted: Fri Mar 13, 2009 10:19 am
by crazycoders
Although untested, but this should lead you into the right direction
^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$
I don't remember if i have to use a + to force 1 or more or if it's implied, check the docs or simply test it, but this should give you your anwser or a good go.
Re: String Pattern
Posted: Fri Mar 13, 2009 10:20 am
by crazycoders
Oh wait i missed the part about not having more than one hyphen at a time...
I don't know if that is possible with a regexp...
Re: String Pattern
Posted: Fri Mar 13, 2009 10:35 am
by vayumahesh
Thanks. That works except for what you said, consecutive hyphens.
I do not understand why we need [a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9] instead of [a-zA-Z0-9][-]*[a-zA-Z0-9] . Can you explain the code.
Re: String Pattern
Posted: Fri Mar 13, 2009 10:50 am
by crazycoders
[a-zA-Z0-9][-]*[a-zA-Z0-9] would allow only one (letter, number) then any number of hyphen then one (letter, number)
So no, it's not right to use it that way.
My way, if i'm not wrong states:
[a-zA-Z0-9] One letter or number
[a-zA-Z0-9-] As many times as needed a letter, number or hyphen
[a-zA-Z0-9] One letter or number
So the first char must be a letter or number
The chars in the middle can be any of letter or number or hyphen and as many times as required...
So the last char must be a letter or number
To solve your "no more than one hyphen at a time" rule, you need to work on the middle part for sure, i just don't know what to do...
Re: String Pattern
Posted: Fri Mar 13, 2009 11:11 am
by vayumahesh
Thank you very much. I missed couple of other possibilities when I mentioned example strings.
A single alphabet or a Number like for example, B and 5. The current regular expression fails with these.
Re: String Pattern
Posted: Fri Mar 13, 2009 12:22 pm
by vayumahesh
I think it is not possible to come up with one single regular expression as a solution for the above requirement.
So, I am validating based on two regular expressions.
1) ^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$ (all other conditions)
2) ^[a-zA-Z0-9]$ (For a single alphabet or number)
Still I am not sure how to force NO consecutive hyphens.
Re: String Pattern
Posted: Fri Mar 13, 2009 1:02 pm
by crazycoders
With a third one you could do it
[AZaz09]*[-]+[AZaz09]*
This will check for more than one consecutive hyphens in a text that optionaly starts or ends with one or more letters/numbers.
Didn't test but should be good
edit: if ereg or preg returns false, it means it didn't not find two consecutive hyphens...
Re: String Pattern
Posted: Sun Mar 15, 2009 6:35 am
by prometheuzz
vayumahesh wrote:...
Dude, what is wrong with you. You have posted this crap in more than 3 different forums now. You're wasting people's time!
Re: String Pattern [[Solved]]
Posted: Sun Mar 15, 2009 8:30 am
by vayumahesh
I did not get any reply from the other two forums. Since this is my first post on Regular expressions, I was not sure which site is the best for regular expressions stuff.
Why do you call it crap ? It is a genuine question. No need to respond, I have marked this thread as Solved. Thanks for your time.
Re: String Pattern [[Solved]]
Posted: Sun Mar 15, 2009 8:32 am
by prometheuzz
vayumahesh wrote:I did not get any reply from the other two forums. Since this is my first post on Regular expressions, I was not sure which site is the best for regular expressions stuff.
Why do you call it crap ? It is a genuine question. No need to respond, I have marked this thread as Solved. Thanks for your time.
What are you talking about? In both forums you were answered! Time waster!
Re: String Pattern
Posted: Mon Mar 16, 2009 7:54 am
by vayumahesh
I was answered on only one site. I forgot to check for email notifications and I was just waiting for email, my mistake. I should have waited for more time for response before posting on this site. sorry about that.

Re: String Pattern
Posted: Mon Mar 16, 2009 8:01 am
by prometheuzz
vayumahesh wrote:I was answered on only one site. I forgot to check for email notifications and I was just waiting for email, my mistake. I should have waited for more time for response before posting on this site. sorry about that.

Fair enough.
Let bygones be bygones.
Regards,
Bart.