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 quick solution with regular expressions?
I used Sean's solution and changed it slightly:
LinkedList<String> numbers = new LinkedList<String>();
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(line);
while (m.find()) {
numbers.add(m.group());
}
... prints
-2
and12
.-? 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.This is for extracting numbers retaining the decimal
Using Java 8, you can do:
If you don't have negative numbers, you can get rid of the
replaceAll
(and use!s.isEmpty()
infilter
), as that's only to properly split something like2-34
(this can also be handled purely with regex insplit
, but it's fairly complicated).Arrays.stream
turns ourString[]
into aStream<String>
.filter
gets rid of the leading and trailing empty strings as well as any-
that isn't part of a number.mapToInt(Integer::parseInt).toArray()
callsparseInt
on eachString
to give us anint[]
.Alternatively, Java 9 has a Matcher.results method, which should allow for something like:
As it stands, neither of these is a big improvement over just looping over the results with
Pattern
/Matcher
as shown in the other answers, but it should be simpler if you want to follow this up with more complex operations which are significantly simplified with the use of streams.You can actually replace [0-9] with \d, but that involves double backslash escaping, which makes it harder to read.
What about to use
replaceAll
java.lang.String method:Output:
Description
+
Between one and unlimited times, as many times as possible, giving back as needed-?
One of the characters “-?”0-9
A character in the range between “0” and “9”Extract all real numbers using this.