Java RegEx – Extract Numbers from String example shows how to extract numbers from string using Java regex. It also shows how to extract negative and decimal numbers using regex.
How to extract numbers from a string using regex in Java?
Extracting all numeric data from the string content is a very common and useful scenerio and can be achieved using a regex pattern. The basic pattern we need to use is a “[0-9]”, i.e. character class of digits from 0 to 9. It can also be written as “\d” and means the same.
Now since the numbers can be more than 1 in length, we are going to append “+” at the end of the pattern to match the digits more than 1 in length. Here is the full regex pattern.
1 |
\\d+ |
Where,
1 2 |
\\d - Any digit from 0 to 9 + - One or more times |
Let’s try this out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javacodeexamples.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExExtractNumbers { public static void main(String[] args) { String strContent = "100 divided by 5 is 20"; String strPattern = "\\d+"; Pattern pattern = Pattern.compile(strPattern); Matcher matcher = pattern.matcher(strContent); while( matcher.find() ) { System.out.println( matcher.group() ); } } } |
Output
1 2 3 |
100 5 20 |
The above-given pattern does not work for the negative numbers. In order to do that, we need to include the minus sign in the pattern. However, it should be optional as only negative numbers have it at the start. Here is the updated regex pattern.
1 |
-?\\d+ |
Where,
1 2 3 4 |
- - Minus sign ? - zero or one time \\d - any digit from 0 to 9 + - One or more times |
Let’s see an example of that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javacodeexamples.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExExtractNumbers { public static void main(String[] args) { String strContent = "-100 divided by 5 is -20"; String strPattern = "-?\\d+"; Pattern pattern = Pattern.compile(strPattern); Matcher matcher = pattern.matcher(strContent); while( matcher.find() ) { System.out.println( matcher.group() ); } } } |
Output
1 2 3 |
-100 5 -20 |
How to extract decimal numbers?
The above-given pattern does not include decimal numbers. In order to include that as well, we are going to use the below-given pattern.
1 |
-?\\d+(\\.\\d+)? |
Where,
1 2 3 4 5 6 |
- - Minus sign ? - zero or one time \\d - any digit from 0 to 9 + - One or more times ()? - Whole group zero or 1 time \\.\\d+ A dot followed by any digit 1 or more times |
Here we have specified the dot and fraction number inside a group that as a whole may appear zero or one time. Let’s try to extract numbers using this pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javacodeexamples.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExExtractNumbers { public static void main(String[] args) { String strContent = "-100 divided by 3 is -33.33"; String strPattern = "-?\\d+(\\.\\d+)?"; Pattern pattern = Pattern.compile(strPattern); Matcher matcher = pattern.matcher(strContent); while( matcher.find() ) { System.out.println( matcher.group() ); } } } |
Output
1 2 3 |
-100 3 -33.33 |
As you can see from the output, the pattern extracted positive, negative, and decimal numbers from the string.
How to extract a specific number from the string?
The above example shows how to extract all numeric data from the string. But what if you want to extract a specific number from the string? Well, in that case, you can define that specific number in a regex group and extract it using the group number as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javacodeexamples.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExExtractNumbers { public static void main(String[] args) { String strContent = "-100 divided by 3 is -33.33"; String strPattern = "is (-?\\d+(\\.\\d+)?)"; Pattern pattern = Pattern.compile(strPattern); Matcher matcher = pattern.matcher(strContent); if( matcher.find() ) { System.out.println( matcher.group(1) ); } } } |
Output
1 |
-33.33 |
If you want to learn more about the regex, please visit the Java regex tutorial.
Please let me know your views in the comments section below.