The Artima Developer Community
Sponsored Link

Java Answers Forum
Regular expressions - how

4 replies on 1 page. Most recent reply: Feb 8, 2006 3:54 AM by Dave Hinton

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
assan rekowski

Posts: 22
Nickname: haiaw
Registered: Sep, 2003

Regular expressions - how Posted: Feb 7, 2006 10:34 AM
Reply to this message Reply
Advertisement
Hi,

I have
String variable= "1234X562";

The variable should contain only numbers...

I would like to use regular expression and validate this variable.

Could anybody show me some example code?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Regular expressions - how Posted: Feb 7, 2006 10:23 PM
Reply to this message Reply
Reead the API of

String.split(...)


Since you don't give any information where the String is stored or where the splitted Strings should go, I can't give you more information than that.

assan rekowski

Posts: 22
Nickname: haiaw
Registered: Sep, 2003

Re: Regular expressions - how Posted: Feb 8, 2006 2:12 AM
Reply to this message Reply
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");

assan rekowski

Posts: 22
Nickname: haiaw
Registered: Sep, 2003

Re: Regular expressions - how Posted: Feb 8, 2006 2:49 AM
Reply to this message Reply
thanks, i found the solution
But what is the difference between [^0-9] and ^[0-9] ?

Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: Regular expressions - how Posted: Feb 8, 2006 3:54 AM
Reply to this message Reply
[^0-9] matches any character that is not a digit.

^[0-9] matches a digit, but only if it is at the start of the string.

Flat View: This topic has 4 replies on 1 page
Topic: need help Previous Topic   Next Topic Topic: How to use Array to get every character from Stack.pop()

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use