Wruktarr
Wruktarr

Reputation: 19

Java - Regex - Replace multiple dots and commas with just one dot

Using TextField to place double/integer numbers, want to change any number with "," to number with "." and remove multiple dots and commas.

String test = "0....,,,,,,,....754568";
test.replaceAll(<?IDK?>, ".");

<?IDK?> means that I don't know what to put there.

Examples:
4,54.5
6,,,,26.2
0....,,,,,,,....7545680.754568
7...27.2

Upvotes: 1

Views: 2818

Answers (2)

psadac
psadac

Reputation: 2342

If you want to replace all . or , by . :

test.replaceAll("[.,]+", ".");

Upvotes: 6

Ouissal
Ouissal

Reputation: 1559

You can have a regular expression, for example, to identify numbers which have more than one dot or comma you can use the following :

\d+[.,]{2,}+\d

You can adjust it based on your needs.

If you want to test it, this website is useful : https://www.freeformatter.com/java-regex-tester.html

Upvotes: 0

Related Questions