How to extract numbers from a string and get an array of ints?

I have a String variable (basically an English sentence with an unspecified number of numbers) and I would like to extract all the numbers into an array of integers. I was wondering if there was a quick solution with regular expressions?




I used Sean's solution and modified it a bit:

LinkedList<String> numbers = new LinkedList<String>(); Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(line); while (m.find()) { numbers.add(m.group()); } 
+100
java string arrays regex
Mar 02 '10 at 22:33
source share
12 answers
 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 leading negative sign - optional. \ d matches a digit, and we must write \ as \\ in Java String. So \ d + matches 1 or more digits.

+162
Mar 02 '10 at
source share

How about using replaceAll java.lang.String method:

  String str = "qwerty-1qwerty-2 455 f0gfg 4"; str = str.replaceAll("[^-?0-9]+", " "); System.out.println(Arrays.asList(str.trim().split(" "))); 

Exit:

 [-1, -2, 455, 0, 4] 



Description

 [^-?0-9]+ 
  • [ and ] delimits the character set for a single match, i.e. only once in any order
  • ^ A special identifier used at the beginning of a set, used to indicate matching all characters that are not in the delimited set, instead of all the characters present in the set.
  • + From one to an unlimited number of times, as many times as possible, returns as needed
  • -? One of the characters "-" and "?"
  • 0-9 ranging from "0" to "9"
+47
Sep 03 '14 at 21:31
source share
 Pattern p = Pattern.compile("[0-9]+"); Matcher m = p.matcher(myString); while (m.find()) { int n = Integer.parseInt(m.group()); // append n to list } // convert list to array, etc 

In fact, you can replace [0-9] with \ d, but this is due to a double backslash, which makes reading difficult.

+18
Mar 02 '10 at
source share
  StringBuffer sBuffer = new StringBuffer(); Pattern p = Pattern.compile("[0-9]+.[0-9]*|[0-9]*.[0-9]+|[0-9]+"); Matcher m = p.matcher(str); while (m.find()) { sBuffer.append(m.group()); } return sBuffer.toString(); 

This is for extracting numbers storing a decimal number

+9
Feb 09 '12 at 23:12
source share

The accepted answer detects the numbers, but does not detect the generated numbers, for example. 2000 and decimal values, for example. 4.8. For such use -?\\d+(,\\d+)*?\\.?\\d+? :

  Pattern p = Pattern.compile("-?\\d+(,\\d+)*?\\.?\\d+?"); List<String> numbers = new ArrayList<String>(); Matcher m = p.matcher("Government has distributed 4.8 million textbooks to 2,000 schools"); while (m.find()) { numbers.add(m.group()); } System.out.println(numbers); 

Conclusion: [4.8, 2,000]

+5
Apr 6 '16 at 17:08
source share

for rational numbers use this: (([0-9]+.[0-9]*)|([0-9]*.[0-9]+)|([0-9]+))

+4
Mar 02 '10 at
source share

Using Java 8, you can do:

 String str = "There 0 are 1 some -2-34 -numbers 567 here 890 ."; int[] ints = Arrays.stream(str.replaceAll("-", " -").split("[^-\\d]+")) .filter(s -> !s.matches("-?")) .mapToInt(Integer::parseInt).toArray(); System.out.println(Arrays.toString(ints)); // prints [0, 1, -2, -34, 567, 890] 

If you don't have negative numbers, you can get rid of replaceAll (and use !s.isEmpty() in the filter ), since this is only for properly splitting something like 2-34 (this can also be handled purely with a regular expression in split , but it's pretty complicated).

Arrays.stream turns our String[] into a Stream<String> .

filter eliminates leading and trailing empty lines, as well as any - which are not part of the number.

mapToInt(Integer::parseInt).toArray() calls parseInt for each String to give us int[] .




Alternatively, in Java 9 there is a Matcher.results method, which should provide something like:

 Pattern p = Pattern.compile("-?\\d+"); Matcher m = p.matcher("There 0 are 1 some -2-34 -numbers 567 here 890 ."); int[] ints = m.results().map(MatchResults::group).mapToInt(Integer::parseInt).toArray(); System.out.println(Arrays.toString(ints)); // prints [0, 1, -2, -34, 567, 890] 



None of them are currently a big improvement over simple cyclic display of results using Pattern / Matcher as shown in other answers, but this should be simpler if you want to perform more complex operations that are greatly simplified by using threads .

+3
Dec 31 '17 at 14:33
source share

I would suggest checking ASCII values ​​to extract numbers from a string. Suppose you have a String input string like myname12345 , and if you just want to extract the numbers 12345 , you can do this by first converting the string to an Array of characters then use the following psuedocode

 for(int i=0;i<CharacterArray.length;i++) { if(a[i]>=48&&a[i]<=58) System.out.print(a[i]); } 

after the numbers are extracted, add them to the array

Hope this helps

+1
May 26 '14 at 19:03
source share

I found this expression the simplest

 String[] extractednums = msg.split("\\\\D++"); 
+1
Jan 09 '18 at 7:31
source share

Extract all real numbers using this.

 public static ArrayList<Double> extractNumbersInOrder(String str){ str+='a'; double[] returnArray = new double[]{}; ArrayList<Double> list = new ArrayList<Double>(); String singleNum=""; Boolean numStarted; for(char c:str.toCharArray()){ if(isNumber(c)){ singleNum+=c; } else { if(!singleNum.equals("")){ //number ended list.add(Double.valueOf(singleNum)); System.out.println(singleNum); singleNum=""; } } } return list; } public static boolean isNumber(char c){ if(Character.isDigit(c)||c=='-'||c=='+'||c=='.'){ return true; } else { return false; } } 
+1
11 Sept. '18 at 10:25
source share

Fraction and grouping symbols for representing real numbers may differ depending on the language. The same real number can be written differently depending on the language.

Number two million in German

2,000,000.00

and in English

2,000,000.00

The method of fully extracting real numbers from a given string in a language-independent way:

 public List<BigDecimal> extractDecimals(final String s, final char fraction, final char grouping) { List<BigDecimal> decimals = new ArrayList<BigDecimal>(); //Remove grouping character for easier regexp extraction StringBuilder noGrouping = new StringBuilder(); int i = 0; while(i >= 0 && i < s.length()) { char c = s.charAt(i); if(c == grouping) { int prev = i-1, next = i+1; boolean isValidGroupingChar = prev >= 0 && Character.isDigit(s.charAt(prev)) && next < s.length() && Character.isDigit(s.charAt(next)); if(!isValidGroupingChar) noGrouping.append(c); i++; } else { noGrouping.append(c); i++; } } //the '.' character has to be escaped in regular expressions String fractionRegex = fraction == POINT ? "\\." : String.valueOf(fraction); Pattern p = Pattern.compile("-?(\\d+" + fractionRegex + "\\d+|\\d+)"); Matcher m = p.matcher(noGrouping); while (m.find()) { String match = m.group().replace(COMMA, POINT); decimals.add(new BigDecimal(match)); } return decimals; } 
+1
Oct 18 '18 at 12:58
source share

If you want to exclude numbers contained in words, such as bar1 or aa1bb, add the \ b word boundaries to any of the regular expression answers. For example:

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

displays:

 2 12 
0
Apr 19 '19 at 0:38
source share



All Articles