Results 1 to 13 of 13
  1. #1
    wwuster is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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:

    Java 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 02:28 PM.

  3. #3
    wwuster is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    wwuster is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote 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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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:

    Java Code:
    String regex = "(?<=[\\d])(,)(?=[\\d])";
    something like:
    Java 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!

  7. #7
    wwuster is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks, that works. I need to learn about look aheads now that I know what's involved.

    William

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    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.
    Java 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 03:25 PM. Reason: Correct error

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    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.
    Java 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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks Darryl, you're the man!

  11. #11
    MrBCut is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Getting rid of commas in large numbers?

    Hi all. I'm a new member. I just wanted to thank you all for this solution! I was beating my head on this problem all day lol. I went from string handling library and methods then it let me to RegEx and then i gave up and typed in "how to get rid of commas in large numbers" and it brought me here.

    I am a member and a fan now and just want to thank you guys for this solution that apparently works for me:

  12. #12
    MrBCut is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Getting rid of commas in large numbers?

    ...by the way, i have found a lot of java reg expression tutorials on line - a lot of them were not so good.

    Can anyone recommend a good resource?

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Getting rid of commas in large numbers?

    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    In future, when you have a question, ask it in a new thread -- they're free. Don't resurrect old dead threads, and especially not with a new, different question.

    The question in this thread was about removing commas from a String -- not about where to find a good regex tutorial.

    Closing the thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. parsing/storing large text data
    By hkansal in forum New To Java
    Replies: 4
    Last Post: 10-19-2008, 06:34 PM
  2. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  3. Need to find large files and folder on the PC. What app needed?
    By Cleaner007 in forum Reviews / Advertising
    Replies: 1
    Last Post: 09-30-2008, 07:06 PM
  4. Eclipse with VERY LARGE source trees
    By wyrickre in forum Eclipse
    Replies: 0
    Last Post: 02-01-2008, 02:23 AM
  5. Replies: 1
    Last Post: 07-26-2007, 07:28 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
  •