Aman Mohammed
Aman Mohammed

Reputation: 2958

Regex or Split to get data in Java

I have the following type of data (separated by space):

I want to split the data such that I can get the strings into one String variable and the numbers to be stored and mapped to other variables inside an object.

I have tried using split and regex in java, however I was not able to crack it.

Matcher matcher = Pattern.compile("\\w+.").matcher(string);

Above only returns the first string.

Matcher matcher1 = Pattern.compile("\\d+.").matcher(string);

This one returns just the first number.

String str = data.text().split("[0-9]")[0];

This one gets me the text strings however i am not sure how to get the numbers.

I am expecting all strings to be separated from the numbers so that I can map the strings to other attributes in the class and the numbers to separate attributes in the class.

Any help would be appreciated! Thanks in advance.

Upvotes: 1

Views: 150

Answers (2)

GBlodgett
GBlodgett

Reputation: 12819

You can use the find() method to keep looping while there is still a match in a String:

while(matcher.find()) {
    System.out.println(matcher.group());
}

Here is an example of putting the results into an ArrayList:

String string = "String1 String2 12 348 51 34 567";
ArrayList<String> strArr = new ArrayList<>();
ArrayList<Integer> numArr = new ArrayList<>();
Matcher matcher = Pattern.compile("\\w+.").matcher(string);
while(matcher.find()) {
    strArr.add(matcher.group());
}
Matcher matcher2 = Pattern.compile("\\d+").matcher(string);
while(matcher.find()) {
    numArr.add(Integer.parseInt(matcher.group().trim()));
}
strArr.stream().forEach(System.out::println);
numArr.stream().forEach(System.out::println);

With the output (for the String "String1 String2 12 348 51 34 567")

String1 
String2 
12 
348 
51 
34 
567

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201457

Instead of trying to do everything with one regular expression, I would break it down into two operations. First, match the five values on the right hand side. Then you can find the index of that substring to find where the desired String data ends. Something like

String[] arr = { "String1 String2 12 348 51 34 567", "StringAlone 12 348 51 34 567",
        "ManyString ManyString1 ManyString2 12 348 51 34 567" };
Pattern right = Pattern.compile("(\\d+ \\d+ \\d+ \\d+ \\d+)$");
for (String data : arr) {
    Matcher m = right.matcher(data);
    if (m.find()) {
        String token = m.group();
        String str = data.substring(0, data.indexOf(token) - 1);
        System.out.println(str);
        int[] numbers = Arrays.stream(token.split(" "))
                .mapToInt(Integer::parseInt).toArray();
        System.out.println(Arrays.toString(numbers));
    }
}

I get

String1 String2
[12, 348, 51, 34, 567]
StringAlone
[12, 348, 51, 34, 567]
ManyString ManyString1 ManyString2
[12, 348, 51, 34, 567]

Upvotes: 1

Related Questions