Skip to content

Instantly share code, notes, and snippets.

@aanoaa
Created August 3, 2010 04:33
Show Gist options
  • Save aanoaa/505831 to your computer and use it in GitHub Desktop.
Save aanoaa/505831 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.util.regex.*;
public class CurrencyRegex {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(toCurrency(arg));
}
}
public static String toCurrency(String str_num) {
Pattern p = Pattern.compile("(?<!\\.\\d)(?<=\\d)(?=(?:\\d\\d\\d)+(?!\\d))");
Matcher m = p.matcher(str_num);
StringBuffer sb = new StringBuffer();
while(m.find()) {
m.appendReplacement(sb, ",");
}
m.appendTail(sb);
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment