Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher("There are more than -2 and less than 12 numbers here");
while (m.find()) {
  System.out.println(m.group());
}

... prints -2 and 12.


-? matches a leading negative sign -- optionally. \d matches a digit, and we need to write \ as \\ in a Java String though. So, \d+ matches 1 or more digits.

Answer from Sean Owen on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @sujathamudadla1213 โ€บ how-to-extract-numbers-from-a-long-string-in-java-b61ccbf85b76
How to extract numbers from a long string in Java | by Sujatha Mudadla | Medium
July 17, 2023 - public static String extractNumbers(String input) { String[] words = input.split(โ€œ\\s+โ€); StringBuilder numbers = new StringBuilder(); for (String word : words) { try { int number = Integer.parseInt(word); numbers.append(number).append(โ€œ ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-extract-numbers-from-a-string-in-Java
How to extract numbers from a string in Java - Quora
Answer (1 of 3): You can extract numbers from a string in Java using two methods. Method 1: Using the in-built method Character.isDigit() The Character.isDigit() method determines whether the character is a digit or not. So you traverse the string, get each character one by one and check if it ...
Discussions

java - How to extract numbers from a string and get an array of ints? - Stack Overflow
I have a String variable (basically an English sentence with an unspecified number of numbers) and I'd like to extract all the numbers into an array of integers. I was wondering whether there was a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Extract digits from string - StringUtils Java - Stack Overflow
(for those who will ask, I have ... specific number within it) I would like to use the StringUtils class from Apache commomns. ... Apache StringUtils. Not sure why you wanna use it though. ... That's what I have, was wondering if maybe there is a function already doing something like I need, but I am not so familiar with it ... Cuz, when you can do it with String itself, you won't need the StringUtils from Apache. ... This question is similar to: Extract digits from a string in Java... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to extract numbers from string in java - Stack Overflow
A program that I write recives an input like this W 12.1 -1 2.2 B 1.2 3.2 1 And I need to check if the numbers are within coonstraints, so my idea is to store those numbers in array as integers. ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
how to extract numeric values from input string in java - Stack Overflow
How can I extract only the numeric values from the input string? For example, the input string may be like this: String str="abc d 1234567890pqr 54897"; I want the numeric values only i.e, "12345... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
DevQA
devqa.io โ€บ extract-numbers-string-java-regular-expressions
Extract Numbers From String Using Java Regular Expressions
February 11, 2020 - import java.util.regex.Matcher; ... } } ... If you want to extract only certain numbers from a string you can provide an index to the group() function....
๐ŸŒ
IncludeHelp
includehelp.com โ€บ java-programs โ€บ extract-digits-numbers-from-string.aspx
Java program to extract digits/ numbers from the string
import java.util.Scanner; class ....print("Enter string that contains numbers: "); str=SC.nextLine(); //extracting string numbers=str.replaceAll("[^0-9]", ""); System.out.println("Numbers are: " + numbers); } }...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-extract-digits-from-a-given-integer
Java Program to Extract Digits from A Given Integer - GeeksforGeeks
July 23, 2025 - Convert the input into string data. Traverse through the string and print the character at each position that is the digits of the number. Below is the implementation of the above approach: ... // Java program to Extract Digits from A Given ...
Find elsewhere
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ find all numbers in a string in java
Find All Numbers in a String in Java | Baeldung
June 24, 2025 - In this article, weโ€™ll use regular expressions to find and extract numbers in strings. Weโ€™ll also cover some ways to count digits. Letโ€™s start by counting the digits found within a string. We can use Java Regular Expressions to count the number of matches for a digit.
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ java-extract-digits-from-string
How to extract digits from a string in Java
February 22, 2020 - The following example shows how you can use the replaceAll() method to extract all digits from a string in Java: // string contains numbers String str = "The price of the book is $49"; // extract digits only from strings String numberOnly = ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ extract-all-integers-from-the-given-string-in-java
Extract all integers from the given String in Java - GeeksforGeeks
Apart from regular expressions, there are other effective methods to extract integers from a string. We will dicuss them below: This method iterates through each character in the string by identifying numeric characters, and building numbers step by step. ... // Java program to extract integers from a given string public class Extract { public static void main(String[] args) { String s = "abc123def456ghi"; StringBuilder r = new StringBuilder(); boolean wasDigit = false; // Flag to track if the previous character was a digit // Iterate through each character in the string for (char c : s.toChar
Published: July 12, 2025
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ how-to-extract-numeric-values-from-input-string-in-java
How to Extract Numeric Values from a String in Java: Tutorial with Examples โ€” javaspring.net
Use java.util.regex.Pattern and Matcher to define patterns and find matches. ... import java.util.regex.*; import java.util.ArrayList; import java.util.List; public class RegexExample { public static List<String> extractNumbersWithRegex(String input) { List<String> numbers = new ArrayList<>(); // Pattern to match integers, decimals, and negatives Pattern pattern = Pattern.compile("[-+]?\\d*\\.?\\d+"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { // Find all matches numbers.add(matcher.group()); // Add matched number to list } return numbers; } public static void main(String[] args) { String input = "Order 456: Total $-199.99, Quantity +3.5"; List<String> numbers = extractNumbersWithRegex(input); System.out.println("Extracted numbers: " + numbers); // Output: [456, -199.99, +3.5] } }
๐ŸŒ
Silicon Cloud
silicloud.com โ€บ home โ€บ extract numbers from string in java: 2 methods
Extract Numbers from String in Java: 2 Methods - Blog - Silicon Cloud
August 5, 2025 - public class Main { public static void main(String[] args) { String str = "abc123def456"; StringBuilder sb = new StringBuilder(); for (char c : str.toCharArray()) { if (Character.isDigit(c)) { sb.append(c); } } String numbers = sb.toString(); System.out.println(numbers); } } The choice between these two methods for extracting numbers from a string depends on the specific requirements and format of the string. #Java programming basics #java regex tutorial #Java string manipulation #java string parsing #number extraction java
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 45266276 โ€บ how-to-extract-numbers-from-string-in-java
How to extract numbers from string in java - Stack Overflow
Split on the delimiter (whitespace, split("\\s")) and then process each element and check whether it is a number. You can do so manually or with a simple regex match like -?\\d+\\.?\\d+ or by trying to parse it with Integer#parseInt, ...
๐ŸŒ
StackHowTo
stackhowto.com โ€บ home โ€บ java โ€บ how to extract numbers from an alphanumeric string in java
How to extract numbers from an alphanumeric string in Java - StackHowTo
October 12, 2021 - In this tutorial, we are going to see how to extract integers from a string in Java. Here is the steps: Step 1: Replace all non-numeric characters with spaces. Step 2: Replace each group of consecutive spaces with a single space.
๐ŸŒ
Tutorjoes
tutorjoes.in โ€บ Java_example_programs โ€บ extract_numbers_from_the_string_in_java
Write Java program to Extract Numbers from the string
System.out.println() method, along with a descriptive message. import java.util.Scanner; class Extract_Numbers { public static void main(String[] args) { String str, num; Scanner input = new Scanner(System.in); System.out.print("Enter the Paragraphs : "); str = input.nextLine(); num = ...
๐ŸŒ
StackHowTo
stackhowto.com โ€บ home โ€บ java โ€บ how to extract numbers from a string with regex in java
How to extract numbers from a string with regex in Java - StackHowTo
October 12, 2021 - For example, if we want to extract only the second number of the string โ€œstr54776str917str78001strโ€, which is 917, we can use the following code: import java.util.regex.*; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("[^\\d]*[\\d]+[^\\d]+([\\d]+)"); Matcher matcher = pattern.matcher("str54776str917str78001str"); if (matcher.find()) { // second matching number System.out.println(matcher.group(1)); } } }
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ -extract-number-from-string-java
How to Extract a Number from a String in Java? - CodingTechRoom
Mistake: Ignoring multiple numbers in a string and only retrieving the first match. Solution: Use a loop with matcher.find() to extract all occurrences. ... A broad desk reference for the Java language and standard library.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ java-program-extract-digits-given-integer
Java Program to Extract Digits from a Given Number - Sanfoundry
May 23, 2022 - Here is the source code of the Java Program to Extract Digits from A Given Integer. The Java program is successfully compiled and run on a Windows system. The program output is also shown below. ... $ javac Extract_Digits.java $ java Extract_Digits Enter any number:5678 Digits at position 4:8 Digits at position 3:7 Digits at position 2:6 Digits at position 1:5
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-extract-numbers-from-a-string-using-regular-expressions
How to extract numbers from a string using regular expressions?
November 21, 2019 - You can match numbers in the given string using either of the following regular expressions โˆ’ ยท โ€œ\d+โ€ Or, "([0-9]+)" import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExtractingDigits { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter sample text: "); String data = sc.nextLine(); //Regular expression to match digits in a string String regex = "\d+"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Creating a Matcher object Matcher matcher = pattern.matcher(data); System.out.println("Digits in the given string are: "); while(matcher.find()) { System.out.print(matcher.group()+" "); } } } Enter sample text: this is a sample 23 text 46 with 11223 numbers in it Digits in the given string are: 23 46 11223 ยท