String Pattern
Moderator: General Moderators
-
vayumahesh
- Forum Newbie
- Posts: 6
- Joined: Fri Mar 13, 2009 10:08 am
String Pattern
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
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
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: String Pattern
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.
^[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.
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: String Pattern
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...
I don't know if that is possible with a regexp...
-
vayumahesh
- Forum Newbie
- Posts: 6
- Joined: Fri Mar 13, 2009 10:08 am
Re: String Pattern
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.
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.
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: String Pattern
[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...
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...
-
vayumahesh
- Forum Newbie
- Posts: 6
- Joined: Fri Mar 13, 2009 10:08 am
Re: String Pattern
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.
A single alphabet or a Number like for example, B and 5. The current regular expression fails with these.
-
vayumahesh
- Forum Newbie
- Posts: 6
- Joined: Fri Mar 13, 2009 10:08 am
Re: String Pattern
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.
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.
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: String Pattern
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...
[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...
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: String Pattern
Dude, what is wrong with you. You have posted this crap in more than 3 different forums now. You're wasting people's time!vayumahesh wrote:...
-
vayumahesh
- Forum Newbie
- Posts: 6
- Joined: Fri Mar 13, 2009 10:08 am
Re: String Pattern [[Solved]]
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.
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.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: String Pattern [[Solved]]
What are you talking about? In both forums you were answered! Time waster!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.
-
vayumahesh
- Forum Newbie
- Posts: 6
- Joined: Fri Mar 13, 2009 10:08 am
Re: String Pattern
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. 
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: String Pattern
Fair enough.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.
Let bygones be bygones.
Regards,
Bart.