3

I want to match currency with or without any thousand separators and or decimals.

String example

String part =  "R4,491.25"; // must be able to match more ex : R12,550,000.60

What i have

if( part.matches("\\R\\d*[.,\\s\\d*]+")){
   System.out.println("C [" + part + "]");
}

Found this Regex for number with decimals and thousand separator and Python regex to match currency with or without comma or decimal

But neither accomplishes what i need . in Javascrpt using regex101 my example seems to work https://regex101.com/r/rK2jMU/4

How can i improve or change my regex to allow for the requirements as stated above?

Thanks in advance.

Community
  • 1
  • 1
Tinus Jackson
  • 3,397
  • 2
  • 25
  • 58

1 Answers1

2

Your regex matches a line break (\R matches a line break in Java 8 regex) as the first symbol.

So, the fix will look like

part.matches("R\\d*[.,\\s\\d*]+")

You might want to try another, more precise regex (that will not allow empty whole number part though):

"R(?:\\d{1,3}(?:[\\s,]\\d{3})+|\\d+)(?:\\.\\d+)?"

Here is this regex demo fiddle

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563