Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular expression

Posted on 2004-04-19
15
Medium Priority
?
308 Views
Last Modified: 2010-03-31
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
Comment
Question by:Magic55
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10858324
[a-z]*[[a-z-]+]*{1,10}

Very wild guess ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10858333
Try

^[a-z]+\\-*|\\-*[a-z]+|[a-z]+\\-*{1,10}$
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10858336
Sorry - that should have been

"^[a-z]+-*[a-z]+|-*[a-z]+|[a-z]+-*{1,10}$";
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:Magic55
ID: 10858471
CEHJ and mayankeagle none of your suggestions worked.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10858497
The last one i posted works fine for me given your specification
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10858512
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
 
LVL 4

Author Comment

by:Magic55
ID: 10858596
strings longer than 10 is true

test
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

everyting else work

/ TK
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 10858670
Yes - you're right! Kludge it:

int length = s.length();
if (length >= 1 && length <= 10 && s.matches(RE)) {
    // do it
}
0
 
LVL 4

Author Comment

by:Magic55
ID: 10858685
The string length check has to be in the regular expression. (It's a part of a much larger expression)
/ TK
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10858708
Why?
0
 
LVL 4

Author Comment

by:Magic55
ID: 10858806
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
 
LVL 2

Accepted Solution

by:
timbauer earned 100 total points
ID: 10863933
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
 
LVL 2

Expert Comment

by:timbauer
ID: 10863953
Forgot to mention, we allow the padding characters (the b's )
to be nullable. This allows us to accept strings < 10 characters.
0
 
LVL 4

Author Comment

by:Magic55
ID: 10866758
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10866778
8-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question