Regex for all decimal numbers to match English format

user10608855

I do have a code that is responsible for replacing all String decimal values with comma separator into String decimal value with point separator, but not all cases are covered.

value.replaceAll(",(?=[0-9]+,)", "").replaceAll(",", ".")

52,52d => 52.52d

65.00f => 65.00f

123,50 => 123.50

1,234,567,89 => 1234567.89

1,234,567.89 => 1234.567.89 (BAD), should be 1234567.89

The fourth bird

You might first replace all the last comma's with a dot, and then replace the rest of the comma's with an empty string.

,(?=\d+[df]?(?!\S))
  • , Match literally
  • (?= Positive lookahead, assert what is directly to the right is
    • \d+[df]? Match 1+ digits and optional d or f
    • (?!\S) Assert a whitspace boundary to the right
  • ) Close lookahead

Example code:

String s = "52,52d\n"
        + "65.00f\n"
        + "123,50\n"
        + "1,234,567,89\n"
        + "1,234,567.89";

System.out.println(s
        .replaceAll(",(?=\\d+[df]?(?!\\S))", ".")
        .replaceAll(",", "")
);

Output

52.52d
65.00f
123.50
1234567.89
1234567.89

See a Java demo


If there is more text that can contain a comma and you don't want to replace those as a side effect, you can also first match all the parts where you want to do the replacements on and for those matches do the replacements.

\b\d+(?:\,\d+)*(?:\.\d+)?[fd]?\b

Regex demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Match numbers (decimal and whole) with Regex

Regex to match Hebrew and English characters except numbers

Want to match all non numbers is Arabic and English

Regex match also negative decimal numbers

regex expression to match decimal numbers with comma as a separator

Regex Match Return all Numbers

Regex Replace all but Numbers and Decimal Place

Regex to match all single or double digit numbers

Regex - Match all numbers after a character

Regex to match lines having specific count of Decimal numbers

JavaScript regex to match optional decimal numbers with optional metric identifier

Match decimal numbers

SQL , format all numbers to 2 decimal places (eg 20 to 20.00)

Regex Match all numbers and hyphen if hyphen is in between numbers

Localized decimal numbers format

Python Regex match all occurrences of decimal pattern followed by another pattern

Regex for Numbers with Optional Decimal

Regex to detect numbers but not decimal

Regex to replace decimal number and all period in currency format

Matching all numbers with regex using preg_match_all

Regex to match non-negative numbers, no leading zeros, 2 decimal places, at least 1 digit, optional decimal

Regex for English and Irish Phone Numbers

Regex to match Japanese/English texts

android regex and decimal format

Using a regex to match only decimal numbers but I keep matching non-single-digit numbers

How to match all combinations of numbers in a string that do not start with an English letter in regular matching in Java

Regex to Match All Numbers Except Those in the First Word

regex match numbers that contain spaces but cannot be all spaces?

Match all numbers greater than 36 using regex?