• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

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
0
Magic55
Asked:
Magic55
  • 7
  • 5
  • 2
  • +1
2 Solutions
 
Mayank SAssociate Director - Product EngineeringCommented:
[a-z]*[[a-z-]+]*{1,10}

Very wild guess ;-)
0
 
CEHJCommented:
Try

^[a-z]+\\-*|\\-*[a-z]+|[a-z]+\\-*{1,10}$
0
 
CEHJCommented:
Sorry - that should have been

"^[a-z]+-*[a-z]+|-*[a-z]+|[a-z]+-*{1,10}$";
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Magic55Author Commented:
CEHJ and mayankeagle none of your suggestions worked.
0
 
CEHJCommented:
The last one i posted works fine for me given your specification
0
 
CEHJCommented:
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
0
 
Magic55Author Commented:
strings longer than 10 is true

test
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

everyting else work

/ TK
0
 
CEHJCommented:
Yes - you're right! Kludge it:

int length = s.length();
if (length >= 1 && length <= 10 && s.matches(RE)) {
    // do it
}
0
 
Magic55Author Commented:
The string length check has to be in the regular expression. (It's a part of a much larger expression)
/ TK
0
 
CEHJCommented:
Why?
0
 
Magic55Author Commented:
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
0
 
timbauerCommented:
If you can at all use CEHJ's suggestion, do it.
However, the language is regular and here is the brute force proof.
Let 'a' be the pattern [a-z]
Let 'b' ....                 [a-z-]?
abbbbbbbbbb UNION babbbbbbbbb UNION bbabbbbbbbb UNION  bbbabbbbbbb ... etc
The trick is using the "a" pattern to force an alphabetic character somewhere.
We provide all possible paddings of "b" around each end.

Therefore,
([a-z]([a-z]?){9})|((([a-z]?){1})[a-z](([a-z]?){8}))|((([a-z]?){2})[a-z](([a-z]?){7})) ........ (([a-z]?){9}[a-z])

The is going to produce the most god-awful DFA but, nor is it maintainable, but it will work.
Someone might be able to provide an observation to simplify things, but I cannot off hand.
Also if you know the precedence rules better, you may remove some of the redundant ( ).

Good luck.

0
 
timbauerCommented:
Forgot to mention, we allow the padding characters (the b's )
to be nullable. This allows us to accept strings < 10 characters.
0
 
Magic55Author Commented:
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
0
 
CEHJCommented:
8-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now