简体   繁体   English

从复杂的字符串中检索整数

[英]Retrieve integer from a complex string

I have a code which read the content of a certain file. 我有一个读取特定文件内容的代码。 The file begin like this : 文件开始像这样:

J-549 J-628 J-379 J-073 J-980 vs J-548 J-034 J-127 J-625 J-667\
J-152 J-681 J-922 J-079 J-103 vs J-409 J-552 J-253 J-286 J-711\
J-934 J-367 J-549 J-169 J-569 vs J-407 J-429 J-445 J-935 J-578\

I would like to store each integer in an array of size 270 (number of line) x 10 (integer at each line). 我想将每个整数存储在大小为270(行数)×10(每行整数)的数组中。

I am not using the correct regex. 我没有使用正确的正则表达式。 Here is a piece of my code : 这是我的一段代码:

String strLine;
int[] id = new int[10];//list ID of each line
//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
    // Print the content on the console

    strLine = strLine.replaceAll("J-", "");
    strLine = strLine.replaceAll(" vs ", " ");
    String[] teamString = strLine.split(" ");
    for(int i=0;i<id.length;i++) {
        System.out.print(id[i] + " ");
}

I was thinking of removing the "J-" and " vs " but it seems to be a bad idea. 我当时想删除“ J-”和“ vs”,但这似乎不是一个好主意。 The console prints : 控制台打印:

549 628 379 073 980 548 034 127 625 667\ 
152 681 922 079 103 409 552 253 286 711\ 
934 367 549 169 569 407 429 445 935 578\ 

Does someone can help me to solve my problem. 有人可以帮助我解决我的问题吗? Thanks! 谢谢!

Instead of replacing all the characters you don't want, you could just use a Regex to match those which you do want. 除了替换所有不需要的字符外,您还可以使用正则表达式来匹配所需的字符。

Pattern idPattern = Pattern.compile("J-(\\d+)");
String strLine;
int[] id = new int[10]; // list ID of each line

// read file line by line
while ((strLine = br.readLine()) != null) {
    Matcher lineMatcher = idPattern.matcher(strLine);

    // find and parse every ID on this line
    for (int i = 0; matcher.find() && i < id.length; i++) {
        String idStr = matcher.group(1); // Gets the capture group 1 "(\\d+)" - group 0 is the entire match "J-(\\d+)"
        int id = Integer.parseInt(idStr, 10);
        id[i] = id;
        System.out.print(id + " ");
    }

    System.out.println();
}

The regex J-(\\d+) matches a part of string which starts with "J-" and ends in one or more numbers. 正则表达式J-(\\d+)与以“ J-”开头并以一个或多个数字结尾的字符串的一部分匹配。 The parentheses around the number create a capture group , which we can access directly instead of having to replace "J-". 数字周围的括号创建了一个捕获组 ,我们可以直接访问该捕获组 ,而不必替换“ J-”。

If you're sure that you want to parse the IDs as integers, notice that when parsed, "073" becomes 73. Not sure if that makes a difference for you. 如果确定要将ID解析为整数,请注意,在解析时,“ 073”变为73。不确定这是否对您有所帮助。 Also, if there may be more than 10 IDs on one line, use an ArrayList and add the IDs there instead of placing them in a fixed-size array. 另外,如果一行上的ID 可能超过10个,请使用ArrayList并在其中添加ID,而不是将它们放在固定大小的数组中。

for replacing \\ use escaping char \\ 用于替换\\使用转义字符\\

String s[] ="J-549 J-628 J-379 J-073 J-980 vs J-548 J-034 J-127 J-625 J-667\\"
                    .replace("\\", "")
                    .replace("J-", "")
                    .replace(" vs ", " ")
                    .split(" ");
//System.out.println(s);
int[] id = new int[10];
for(int i=0; i<s.length; i++) {
    id[i] = Integer.valueOf(s[i]);
}
for(int i=0; i<id.length; i++) {
    System.out.println(id[i]);
}

Result: 结果:

549
628
379
73
980
548
34
127
625
667

Simply Use regex of \\d+ to capture the number terms and retrieve all them to a List or array whatever you want. 只需使用\\d+正则表达式捕获数字项,然后将所有它们检索到列表或数组即可。

 Matcher m = Pattern.compile("\\d+").matcher(strLine);

 while (m.find()) {
    // print the number or store it in array or whatever
    System.out.println(m.group());
 }

use strLine.replace("\\\\", ""); 使用strLine.replace("\\\\", ""); one more time. 再一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM