Vous pouvez utiliser regex, donc si votre regard votre numéro existant entre sample
et space
de sorte que vous pouvez utiliser ceci:
public static final String REGEX_START = Pattern.quote("sample ");
public static final String REGEX_END = Pattern.quote(" ");
public static final Pattern PATTERN = Pattern.compile(REGEX_START + "(.*?)" + REGEX_END);
public static void main(String[] args) {
String input = "This is a sample 123 string 456. And continue 123...";
List<String> keywords = new ArrayList<>();
Matcher matcher = PATTERN.matcher(input);
// Check for matches
while (matcher.find()) {
keywords.add(matcher.group(1));
}
keywords.forEach(System.out::println);
}
vous pouvez également utiliser la solution de @Peter Lawrey supprimer juste le *
Pattern PATTERN = Pattern.compile("sample.(\\d+)");
Matcher matcher = PATTERN.matcher(input);
// Check for matches
while (matcher.find()) {
keywords.add(matcher.group(1));
}
vous pouvez utiliser une expression régulière ' "échantillon. * (\\ d +)"' un matcher. –
Passez par regex..https. //docs.oracle.com/javase/tutorial/essential/regex/ – Akshay
@PeterLawrey votre solution sera de retour '3' à la place, vous devriez d'enlever le' '' "échantillon (\\ d +) "' –