Magic55
asked on
Regular expression
Can you help me with this regular expression? It should match strings with:
- Characters a-z and -
- String size 1 - 10
Like this ^[a-z-]{1,10}$
But it should not match, if the string contains only the character -
How can I stop this?
/ TK
- Characters a-z and -
- String size 1 - 10
Like this ^[a-z-]{1,10}$
But it should not match, if the string contains only the character -
How can I stop this?
/ TK
Try
^[a-z]+\\-*|\\-*[a-z]+|[a- z]+\\-*{1, 10}$
^[a-z]+\\-*|\\-*[a-z]+|[a-
Sorry - that should have been
"^[a-z]+-*[a-z]+|-*[a-z]+| [a-z]+-*{1 ,10}$";
"^[a-z]+-*[a-z]+|-*[a-z]+|
ASKER
CEHJ and mayankeagle none of your suggestions worked.
The last one i posted works fine for me given your specification
Here's the output (true and false indicate a match)
C:\java\dumpit>jr Base ---jka
true
C:\java\dumpit>jr Base ---
false
C:\java\dumpit>jr Base a-s
true
C:\java\dumpit>jr Base a-
true
C:\java\dumpit>jr Base -
false
C:\java\dumpit>jr Base ---jka
true
C:\java\dumpit>jr Base ---
false
C:\java\dumpit>jr Base a-s
true
C:\java\dumpit>jr Base a-
true
C:\java\dumpit>jr Base -
false
ASKER
strings longer than 10 is true
test
aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaa aaaaaa
everyting else work
/ TK
test
aaaaaaaaaaaaaaaaaaaaaaaaaa
everyting else work
/ TK
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The string length check has to be in the regular expression. (It's a part of a much larger expression)
/ TK
/ TK
Why?
ASKER
The actual string that I test look like this
part1.part2.part3 or part1.part3
Part 1 must match the regular expression described in the question.
Then I will put it in a larger expression which also check part 2 and 3
/ TK
part1.part2.part3 or part1.part3
Part 1 must match the regular expression described in the question.
Then I will put it in a larger expression which also check part 2 and 3
/ TK
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Forgot to mention, we allow the padding characters (the b's )
to be nullable. This allows us to accept strings < 10 characters.
to be nullable. This allows us to accept strings < 10 characters.
ASKER
timbauer, I thought of that solution but the problem is that the max size isn't really 10. I only used it in this example. The real max length is 63 which will give a very large expression, but as you said, it works.
I solved it with a separate length check as CEHJ suggested.
It isn't ideal in my application (it would be better if I got all in one expression), but it's better than a huge regular expression.
I will split the points ....
/ TK
I solved it with a separate length check as CEHJ suggested.
It isn't ideal in my application (it would be better if I got all in one expression), but it's better than a huge regular expression.
I will split the points ....
/ TK
8-)
Very wild guess ;-)