Problem with simple regular expression

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Problem with simple regular expression

1. Registered User
Join Date
Sep 2003
Posts
24

## Problem with simple regular expression

String stringTest="555";
Pattern p = Pattern.compile("^[0-9]");
Matcher m=p.matcher(stringTest);
while(m.find()){

}

I want to throw exception if stringTest has not only numbers, but this code always throws RuntimeException("FOUND BAD THING");

2. Registered User
Join Date
Sep 2003
Posts
24
thanks, i found the solution
But what is the difference between [^0-9] and ^[0-9] ?

3. Registered User
Join Date
Dec 2005
Location
New Jersey
Posts
290
Originally Posted by haiaw
thanks, i found the solution
But what is the difference between [^0-9] and ^[0-9] ?
One works and one doesn't. Look at the Pattern class.
Originally Posted by Patter
Character classes
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)
There is no ^[abc].

4. Registered User
Join Date
Feb 2006
Location
Cologne - Germany
Posts
271
particullarly there is a ^[0-9].
^ not within squared brackets and at the start of a regex means to match from beginning of input.
while the regex "abc" matches in "xxabc"
the regex "^abc" doesn't, since the pattern doesn't appear at the beginning of the input.
in square brackets ^ means negotiation.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise