0

It seems like my question is really simple and most likely duplicated but I coudn't find an answer.

Here is my string:

String s = "Bob age 30 height 190 and weight 80";

I need to get it to different field 30, 190 and 80. Age might be 9, height 99 and weight 101 so the method should be flexible.

String age = 30;
String height = 190;
String weight = 80;

How can I extract it?

I need to do it in Telend Open Studio and I there is no possibility to initialize and put it into array and then get it and put it to the string. I want to put 30 in age field and do the same with height and weight.

kek
  • 1
  • 2
  • So you want to extract only the digits from a String? – Nicholas K Feb 22 '19 at 09:52
  • 1
    Would the digit always come directly after the word that describes it? – Kleo G Feb 22 '19 at 09:53
  • 1
    Do strings "age", "height" and "weight" always present before the numbers? Are they the strict condition for matching those values? I imagine they could be in random order. – Andrea Feb 22 '19 at 09:53
  • Yes, but I have to put it into different fields and there might be a numbers with precision like 80.5 – kek Feb 22 '19 at 09:54
  • There is possibility that I will not have these fields at all. But if I have it always look like that. – kek Feb 22 '19 at 09:55
  • Possible duplicate of [How to extract numbers from a string and get an array of ints?](https://stackoverflow.com/questions/2367381/how-to-extract-numbers-from-a-string-and-get-an-array-of-ints) – Nicholas K Feb 22 '19 at 09:56
  • Create 3 [Patterns](https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html) each of which matches [_] with the prefix being either age height or weight @Asier Aranbarri edit: ...and split the resulting String at the whitespace – T A Feb 22 '19 at 09:56
  • this would be a mix of three indexOf ("age"), ("height"),... and an integer parse of the consequent lines (if they exist, and checking if they are numerals) – aran Feb 22 '19 at 09:57
  • So you need to extract only numbers also the fields(like age,height,weight) ? – soorapadman Feb 22 '19 at 09:59
  • @soorapadman I want to extract 30 and put it into **age** field and do the same with other – kek Feb 22 '19 at 10:01

5 Answers5

1

try this one :

import java.util.regex.Pattern;
import java.util.regex.Matcher;

Pattern pattern = Pattern.compile("-?\\d+");
String s = "Bob age 30 height 190 and weight 80";
Matcher matcher = p.matcher(s);
while (matcher.find()) {
  System.out.println(matcher.group());
}

you can add result to a list instead of doing Sysout.

Mohamed Nabli
  • 1,629
  • 3
  • 17
  • 24
1
 String example="Bob age 30 height 190 and weight 80";          
        Pattern pattern = Pattern.compile(".*age\\s(\\d+\\.\\d+|\\d+).*height\\s(\\d+\\.\\d+|\\d+).*weight\\s(\\d+\\.\\d+|\\d+)");
        Matcher matcher = pattern.matcher(example);
        while(matcher.find()) {
           String age = matcher.group(1);
           String height = matcher.group(2);
           String weight = matcher.group(3);
         }
  1. .*one or more characters.
  2. age is a static string.
  3. \s means space
  4. () means grouping match
  5. \d only one digit and + means one or more digits if I write \d+ means more then one digit //eg 1,166,168 and some cases you want 80.5 so i added . and after . there should at least one or more digit so i added \d+.
  6. | means or
  7. \d+ only digit //e.g 80
Akash Shah
  • 596
  • 4
  • 17
0
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Solution {

    public static void main(String[] args) {
        String test="Bob age 30 height 190 and weight 80";
        String number = test.substring(test.lastIndexOf("age") + 3,test.lastIndexOf("age") + 6);
        System.out.print(number);
        number = test.substring(test.lastIndexOf("height") + 6,test.lastIndexOf("height") + 10);
        System.out.print(number);
        number = test.substring(test.lastIndexOf("weight") + 6,test.lastIndexOf("weight") + 9);
        System.out.print(number);

        Pattern p = Pattern.compile("-?\\d+");
        test = "Bob age 30 height 190 and weight 80";
        Matcher m = p.matcher(test);
        System.out.println("\n");
        while (m.find()) {
          System.out.print(" "+m.group());
        }
    }
}
0

There could be a better way than string manipulation to do this.

But if you do want to use String manipulation then you could use a method which searches for the specific key words and gets the location in the sting (String indexOf(age) method), and then retrieving the String directly after that keyword (adding one to the location you just got) ONLY if it finds that substring (if it does not find it, it will not attempt to retrieve the digit).

You should also change height and weight variables to double and when you extract the value from the String use Double.parseDouble(numToParse).

Kleo G
  • 247
  • 1
  • 4
  • 20
0

First of all you can replace all alpha words to space and then split it by space.And take list and store it by using some hard code. Like following:

public static void main( String[] args )
   {
      String line = "Bob age 30 height 190 and weight 80";
      String numbersLine = line.replaceAll("[^0-9]+", " ");
      String[] strArray = numbersLine.split(" ");
      List<String> list = new ArrayList<>();
      int index = 0;
      for (String string : strArray) {
          if (!string.equals("")) {
             if( index == 0){
                list.add("String age = "+Integer.parseInt(string) + ";");
             }
             else if( index == 1){
                list.add("String height = "+Integer.parseInt(string) + ";");
             }else{
                list.add("String weight = "+Integer.parseInt(string) + ";");
             }
             index++;
          }
      }
      for( String str : list)
      {
         System.out.println( str );
      }
   }

Output

String age = 30;
String height = 190;
String weight = 80;
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24