-
Problem with simple regular expression
String stringTest="555";
Pattern p = Pattern.compile("^[0-9]");
Matcher m=p.matcher(stringTest);
while(m.find()){
throw new RuntimeException("FOUND BAD THING");
}
I want to throw exception if stringTest has not only numbers, but this code always throws RuntimeException("FOUND BAD THING");
-
thanks, i found the solution
But what is the difference between [^0-9] and ^[0-9] ?
-
 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].
-
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.
Similar Threads
-
By ac7117 in forum ASP.NET
Replies: 0
Last Post: 01-30-2002, 04:52 PM
-
By Patrick Ireland in forum .NET
Replies: 2
Last Post: 05-05-2001, 03:36 PM
-
Replies: 4
Last Post: 04-13-2001, 03:31 AM
-
By Eric in forum Database
Replies: 1
Last Post: 11-10-2000, 02:05 AM
-
By Gary Thompson in forum authorevents.kurata
Replies: 1
Last Post: 04-20-2000, 08:13 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks