
10-26-2008, 02:14 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 5
Rep Power: 0
|
|
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
|
|

10-26-2008, 02:26 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
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.
|
|

10-26-2008, 03:46 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 5
Rep Power: 0
|
|
|
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
|
|

10-26-2008, 03:48 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
|
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.
|
|

10-26-2008, 05:19 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 5
Rep Power: 0
|
|
Originally Posted by Fubarable
|
|
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
|
|

10-27-2008, 04:03 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
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!
|
|

10-27-2008, 11:33 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 5
Rep Power: 0
|
|
|
Thanks, that works. I need to learn about look aheads now that I know what's involved.
William
|
|

10-27-2008, 02:48 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
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
|
|

10-28-2008, 09:39 PM
|
 |
Senior Member
|
|
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 716
Rep Power: 2
|
|
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
|
|

10-28-2008, 10:47 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
|
Thanks Darryl, you're the man!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 03:07 PM.
|
|