tags:

views:

96

answers:

2

The above regular expression (in Java) matches a string of alpha numeric characters of length between 5 and 10.

How can I modify the above regular expression to match with the above requirements as well as matching with an empty string?

+15  A: 

Make it optional (match exactly one or zero times)

^([a-zA-Z0-9]{5,10})?$
SchlaWiener
Thanks, your idea works and the explanation easy to understand.
MISS_DUKE
No prob, btw. the ? quantifier is just a shortcut for {0,1}
SchlaWiener
+3  A: 

^(?:[a-zA-Z0-9]{5,10}|)$

joni
SchlaWieners is the more elegant solution..
joni
Thanks, your idea seems working, but I do not understand it.
MISS_DUKE
You see the pipe "|" nearly at the end of the regex? It's like an OR operator, in this case ` [a-zA-Z0-9]{5,10} ` OR "" :)
joni