How to extract only specific type values from delimited string using regex in java

Viewed 84

I have a string like below:

SOMETEXT(ABC, DEF, 5, 78.0, MNO)

I want to parse it using regex to get the List<String> of ABC, DEF and MNO. ie. I want to avoid numbers of any type and extract only text.

At large, I have a structure like below:

class Detail {
    String name;
    String type;
}

// Sample values of name = "test1" type = "SOMETEXT(ABC,5)"
// Sample values of name = "test2" type = "SOMETEXT(ABC,DEF,2.2)"
// Sample values of name = "test3" type = "SOMETEXT(ABC,DEF)"

From the List<Detail> I want to get Map<String, List<String>> where list<String> is extracted texts from type and key is name, in java 8 way using streams if possible.

Till now I had to get only first text from the string and i did that as below:

Map<String, List<String>> assignOperatorMap = details
    .stream()
    .collect(groupingBy(md -> md.getName(), mapping((Details m) ->
        m.getType().substring(m.getType().indexOf("(") + 1,
        m.getType().indexOf(")")).split("\\,")[0] , 
        Collectors.toList()
    )));

Above code gives me: {test1=[ABC], test2=[ABC], test3=[ABC]} that is only first value.

2 Answers
Related