I have a string that looks like this:
String a = "name:string, age:int, address:string,city:string, zip:int";
The string is separated by ,
and between :
it is the fieldName
and type
. There might be space between each value but any space should be ignored.
What I want is to convert this string into an arrayList that only contents fieldName
.
So the above example will become
List<String> result = new ArrayList<>;
and in the result there should be values of name, age, address, city, zip
. Everything else should be ignored.
How do i do this with regex in Java?
I can achieve this by first do the split by ,
then split the string array by :
, and take the first element of the array then added to a list.
The reason i m asking is because I want to know if there is a more elegant way to do this with regex.