Reputation: 988
I have a regex:
"(\\d+\\.\\,?)+"
And the value:
3.053,500
But my regex pattern does not match it. I want to have a pattern which validates numbers, dots and commas. For exmaple values which are valid:
1
12
1,2
1.2
1,23,456
1,23.456
1.234,567
etc.
Upvotes: 1
Views: 72
Reputation: 626748
Your (\d+\.\,?)+
regex matches 1 or more repetitions of 1+ digits, a dot, and an opional ,
. It means the strings must end with a dot. 3.053,500
does not end with a dot.
You may use
s.matches("\\d+(?:[.,]\\d+)*")
See the regex demo
Note that the ^
and $
anchors are not necessary in Java's .matches()
method as the match is anchored to the start/end of the string automatically. At regex101.com, the anchors are meant to match start/end of the line (since the demo is run against a multiline string).
Pattern details
\d+
- 1+ digits(?:
- start of a non-capturing group:
[.,]
- a dot or ,
\d+
- 1+ digits)*
- 0 or more repetitions.Upvotes: 2