Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-26-2008, 02:14 PM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
wwuster is on a distinguished road
Default Getting rid of commas in large numbers?
Can someone tell me a regular expression to get rid of commas in a large number using
String.replaceAll()?
For example, change "100,000" to "100000"

thanks,
William
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-26-2008, 02:26 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
Default
I would try to make this Locale-specific by using a NumberFormat object. This way you could easily change this to be able to accommodate other ways of writing numbers (for instance Spain where they use a decimal point where you have a comma here). Something like so:

Code:
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class Fubar001
{
  public static void main(String[] args)
  {
    String s1 = "100,000";
    String s2 = "";
    NumberFormat nf = NumberFormat.getInstance(Locale.getDefault()); // make locale-specific
    try
    {
      int i1 = nf.parse(s1).intValue();
      s2 = String.valueOf(i1);
      System.out.println(s2);
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }
  }
}

Last edited by Fubarable; 10-26-2008 at 03:28 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-26-2008, 03:46 PM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
wwuster is on a distinguished road
Default
These numbers with commas are embedded in existing strings that I didn't create and could be anywhere in the strings, so I need a general way to remove the commas in numbers.

thanks,
William
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-26-2008, 03:48 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
Default
Quote:
so I need a general way to remove the commas in numbers.
So let's first see your code that demonstrates your attempt to solve this, and then we'll then know where to go with this.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-26-2008, 05:19 PM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
wwuster is on a distinguished road
Default
Originally Posted by Fubarable View Post
So let's first see your code that demonstrates your attempt to solve this, and then we'll then know where to go with this.
I'd like to simply use a regular expression in String.replaceAll.

String str = "there are 123,000 blue items and 117,287 red items";
String regex = "???????";
str = str.replaceAll(regex, str);

After this, str would be "there are 123000 blue items and 117287 red items"

I'm just learning regular expressions and don't know how to create such a regular expression. That's my question.

William
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-27-2008, 04:03 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
Default
I'm just learning regex myself, but I think that to solve this you'll need both look aheads and look behinds. You may wish to try this regex String:

Code:
String regex = "(?<=[\\d])(,)(?=[\\d])";
something like:
Code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Fubar002
{
  public static void main(String[] args)
  {
    String regex = "(?<=[\\d])(,)(?=[\\d])";
    Pattern p = Pattern.compile(regex);
    
    String str = "there are 123,000 blue items and 117,287 red items";
    Matcher m = p.matcher(str);

    str = m.replaceAll("");
    System.out.println(str);
  }
}
To Darryl.Burke: Hey, I may be able to learn some of this regex stuff after all!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-27-2008, 11:33 AM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
wwuster is on a distinguished road
Default
Thanks, that works. I need to learn about look aheads now that I know what's involved.

William
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-27-2008, 02:48 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
I took a slightly different approach. I used capture groups to repeatedly scan the String for text before the , the , and text after the ,. The before text must end with digit and the after text must start with 3 digits.
Code:
	public static void main(String[] args) {
		final String data = "This short, quick test for removing , from 1,235,789 and more.";

      Pattern pattern = Pattern.compile("(.*?\\d+)(,)(\\d{3}.*?)"); 
        // Defines 3 groups (in parans):
        // 1 - before the ,
        // 2 - the ,
        // 3 - after the comma
      
      String newData = data;           // Prime the work variable

      while(true) {
         Matcher matcher = pattern.matcher(newData);
         if(matcher.find()) {
            int gc = matcher.groupCount();
            for(int i=0; i <= gc; i++) {
               System.out.println("group" + i + "=" + matcher.group(i)); 
               //group0=This short, quick test for removing , from 1,235
               //group1=This short, quick test for removing , from 1
               //group2=,
               //group3=235
            }
            newData = matcher.replaceFirst("$1$3"); 
            System.out.println(" new=" + newData + "\ndata=" + data);
            // new=This short, quick test for removing , from 1235 and more.
            //data=This short, quick test for removing , from 1,235 and more.
            // new=This short, quick test for removing , from 1235789 and more.  <<< Final result
            //data=This short, quick test for removing , from 1,235,789 and more. 

   
         }else{
            break;                     // Exit loop when no match found
         }
      } // end while()
	} // end main

Last edited by Norm; 10-27-2008 at 04:25 PM. Reason: Correct error
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-28-2008, 09:39 PM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 716
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
I like keeping it as simple as possible. The only condition to match here is a comma preceded and follwed by a digit character.

This is alomost the same as the solution already posted by Fubarable, but uses String#replaceAll which calls the same Pattern and Matcher methods under the hood.
Code:
public class RemoveComma {

   public static void main(String[] args) {
      String regex = "(?<=\\d),(?=\\d)";
      String input = "there are 123,000 blue items and 117,287 red items";

      String output = input.replaceAll(regex, "");
      System.out.println(output);
   }
}
db
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-28-2008, 10:47 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
Default
Thanks Darryl, you're the man!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing/storing large text data hkansal New To Java 4 10-19-2008 07:34 PM
printing two smallest numbers from a series of numbers trofyscarz New To Java 2 10-15-2008 12:46 AM
Need to find large files and folder on the PC. What app needed? Cleaner007 Reviews / Advertising 1 09-30-2008 08:06 PM
Eclipse with VERY LARGE source trees wyrickre Eclipse 0 02-01-2008 03:23 AM
How to extract each record from a large XML doc. which contain multiple nodes renjan XML 1 07-26-2007 08:28 PM


All times are GMT +2. The time now is 03:07 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org