BB [FL:60 BT:10 SG:20 MK:10 | 12] 我在一个文本文件中有上述数据,我需要单独获取整数值
简而言之,第一个BB用":“代表食物类型,用"|”代表制作时间
如何使用文件阅读器在java中获取这5个参数谢谢
发布于 2018-04-19 14:44:49
在这里使用正则表达式匹配器,并迭代您的输入字符串,匹配您的纯数字:
String input = "BB [FL:60 BT:10 SG:20 MK:10 | 12]";
String regex = "\\d+(?=[^0-9.])";
List<Integer> vals = new ArrayList<>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
while (m.find()) {
vals.add(Integer.parseInt(m.group(0)));
}
for (int val : vals) {
System.out.println("Found an integer: " + val);
}
Found an integer: 60
Found an integer: 10
Found an integer: 20
Found an integer: 10
Found an integer: 12
Demo
发布于 2018-04-19 14:56:19
拆分、拆分、修剪和拆分:
-> String code = "BB [FL:60 BT:10 SG:20 MK:10 | 12]"
| Added variable code of type String with initial value "BB [FL:60 BT:10 SG:20 MK:10 | 12]"
-> String[] mattime = code.split ("[\\]\\[\\|]");
| Modified variable mattime of type String[] with initial value [Ljava.lang.String;@2286778
| Update overwrote variable mattime
-> mattime [1]
| Expression value is: "FL:60 BT:10 SG:20 MK:10 "
| assigned to temporary variable $32 of type String
-> String[] elem = mattime [1].split (" ")
| Modified variable elem of type String[] with initial value [Ljava.lang.String;@13a5fe33
| Update overwrote variable elem
-> for (String e: elem) println (e);
FL:60
BT:10
SG:20
MK:10
-> for (String e: elem) {println (e); String [] kv = e.trim().split (":") ; println (kv[0] + " : " + Integer.parseInt (kv[1])); }
FL:60
FL : 60
BT:10
BT : 10
SG:20
SG : 20
MK:10
MK : 10https://stackoverflow.com/questions/49914342
复制相似问题