Quantifier to print only 3 digit numbers in Java regex
What is a quantifier to print only 3-digit numbers per string in Java regex?
Input : 232,60,121,600,1980
Output : 232,121,600
Instead, my output comes in like:
Output : 232,121,600,198
I am using (\\d{3})
. What quantifier should I use to print only three-digit numbers?
+3
user3709312
source
to share
1 answer
You need to use a word border \b
:
\b\d{3}\b
See demo
In Java, use double slashes:
String pattern = "\\b\\d{3}\\b";
You don't need to use a capture group around the whole regex, you can access the match via .group()
. See IDEONE demo
+6
Wiktor Stribiżew
source
to share