Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
String s = "bla\n"
+ "123456789\n"
+ "bla\n"
+ "987654321123456789\n"
+ "bla\n";
Pattern pattern = Pattern.compile(".*?(\\d{9}).*?", Pattern.DOTALL);
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
123456789
987654321
123456789
ich würde gerne wissen, wie ich in einem Text nach einer 9 stelliger Nummer suchen kann.
Wenn dann doch bitte richtig:
Java:String s = "bla\n" + "123456789\n" + "bla\n" + "987654321123456789\n" + "bla\n"; Pattern pattern = Pattern.compile(".*?(\\d{9}).*?", Pattern.DOTALL); Matcher matcher = pattern.matcher(s); while (matcher.find()) { System.out.println(matcher.group(1)); }
Code:123456789 987654321 123456789
private String getNumber(String text) {
Pattern compile = Pattern.compile("(\\d{9})");
Matcher matcher = compile.matcher(text);
while (matcher.find()) {
String group = matcher.group();
if (group != null) {
if (group.length() == 9) {
return group;
}
}
}
return null;
}
private List<String> getNumbers(String text) {
List<String> numbers = new ArrayList<>();
Pattern compile = Pattern.compile("(\\d{9})");
Matcher matcher = compile.matcher(text);
while (matcher.find()) {
String group = matcher.group();
if (group != null) {
if (group.length() == 9) {
numbers.add(group);
}
}
}
return numbers;
}
Sorry großer Quatsch, was ich geschrieben hatte. Es genügt einfach:Also liefert (\\d{9}) genau das was verlangt wurde. Also ist das auch richtig.
String s = "bla\n"
+ "123456789\n"
+ "bla\n"
+ "987654321123456789\n"
+ "bla\n";
Pattern pattern = Pattern.compile("\\d{9}");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}