Problem with simple regular expression


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Problem with simple regular expression

  1. #1
    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()){

    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");

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

  3. #3
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote 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.
    Quote 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. #4
    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.

Similar Threads

  1. vbscript regular expression match HTML Tag
    By ac7117 in forum ASP.NET
    Replies: 0
    Last Post: 01-30-2002, 05:52 PM
  2. Replies: 2
    Last Post: 05-05-2001, 04:36 PM
  3. simple XML APPENDING problem
    By scopee in forum XML
    Replies: 4
    Last Post: 04-13-2001, 04:31 AM
  4. Simple SQL Parameter problem
    By Eric in forum Database
    Replies: 1
    Last Post: 11-10-2000, 03:05 AM
  5. Simple report problem
    By Gary Thompson in forum authorevents.kurata
    Replies: 1
    Last Post: 04-20-2000, 09: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center