import java.util.ArrayList; import java.util.List; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class Main { List<String> list = Pattern.compile("@?\\w+|\\b(?!$)") .matcher(str) .results() .map(MatchResult::group) .collect(Collectors.toList()); // #############Non-Stream Solution############# Matcher matcher = Pattern.compile("@?\\w+|\\b(?!$)").matcher(str); List<String> parts = new ArrayList<>(); while (matcher.find()) { parts.add(matcher.group()); } } }
Standard input is empty
[this, , is, , a, , test, , of, , proper, , nouns, , @Ryan] [this, , is, , a, , test, , of, , proper, , nouns, , @Ryan]