[英]Java regular expression and pattern
I have a outlook message with body. 我有一个关于身体的展望信息。 I need to get strings of certain pattern
S15345,S15366 etc.
我需要获取某些模式的字符串S15345,S15366等。
How can i achieve this in java? 我如何在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 在这种情况下,更好的方法是在此正则表达式
S\\d{5}
使用模式匹配器,或者,如果模式可以包含一个或多个数字,则可以使用S\\d+
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 : 如果您使用的是Java 9+,则可以使用:
String body = ...
List<String> result = Pattern.compile("S\\d{5}")
.matcher(body)
.results()
.map(MatchResult::group)
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.