jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

regular expression

syed quadri

Joined: Nov 11, 2012
Posts: 10

I am facing problem to find out digits using regular expressions. Could anyone please help ?

Is it possible ?

// Code

public static void main(String[] args)throws IOException {
int counter=0;

File f = new File("E:\\abc.txt");
FileWriter br = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(br);
Pattern p = Pattern.compile("\\d");
Matcher m =p.matcher();

Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Hi, and welcome to the Ranch!

It's not clear what you mean by "find out digits". Please TellTheDetails(←click) of what you're trying to accomplish. Some possible interpretations of "find out digits" are:

  • Determine if the string contains any digits at all.
  • Determine if the string consists of only digits.
  • Extract all digits or groups of digits from the string, for use on their own.

  • And there are others.

    In the mean time, note that the regex token to match a single digit is \d, which you would write as in a Java string literal.

    fred rosenberger
    lowercase baba

    Joined: Oct 02, 2003
    Posts: 11956

    I have copied your code into this post and used the code tags. It makes reading your java much easier, and we ask that everyone do this in their own post. If you don't know how to do it, please read this.

    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Jeff Verdegan

    Joined: Jan 03, 2004
    Posts: 6109

    This seems to be a duplicate of http://www.coderanch.com/t/597776/java/java/find-out-digits-spaces-String.

    I agree. Here's the link: http://aspose.com/file-tools
    subject: regular expression
    It's not a secret anymore!