如何从同时包含文本和数字的逐行文本文件中选择数字?
例如:
[10] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou
我只想打印 0 1 2 25 21 35
而不包含 [10]
。但我不断收到 10012252135
。
这是我的代码
try {
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String i = scan.nextLine();
String final_string = "";
for (int j = 0; j < i.length(); j++) {
char myChar = i.charAt(j);
if (Character.isDigit(myChar)) {
final_string = final_string.concat(Character.toString(myChar));
}
}
System.out.println(final_string);
}
scan.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
最佳答案
String tt = "[10] begin0-1-selp-2-yelp-25-jelp-21-hi-35-ou";
tt = tt.replaceAll("\\[.*\\]",""); // IT get rid of any [AnyNumber]
tt = tt.replaceAll("\\D+",""); // It get rid of any char that is not a letter
System.out.println(tt);
我做了一个正则表达式,我不能将其写成一行,但输出是 disered。 输出
012252135
关于java - java中如何读取文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50727222/