I have a outlook message with body. I need to get strings of certain pattern S15345,S15366 etc.
How can i achieve this in java?
I tried giving like below,
String array[] = body.split("[S[0-9]]");
In this case better way is to use Pattern Matcher with this regex S\d{5} or if the pattern can contain one or more digits you can use S\d+ instead
String body = ...
Pattern pattern = Pattern.compile("S\\d{5}");
Matcher matcher = pattern.matcher(body);
List<String> result = new ArrayList<>();
while (matcher.find()){
result.add(matcher.find());
}
If you are using Java 9+ you can use :
String body = ...
List<String> result = Pattern.compile("S\\d{5}")
.matcher(body)
.results()
.map(MatchResult::group)
.collect(Collectors.toList());
body.split(",");?