简体   繁体   中英

Extracting comma-separated values in parenthesis

I have a text file in the following format:

0 (1, 10), (0, 5), (3, 40)  
1 (2, 15), (1, 23), (2, 18), (4, 5), (3, 50)  
2 (3, 100)  

The first number in every line is used for a specific purpose. Similarly, each of the integers in parenthesis is used for another specific purpose. Therefore, I am trying to extract each of these integers separately. This is what I have tried:

BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line = br.readLine();
while (line != null) {
     String[] separate = line.split(", ");
     for (int i = 0; i < patterns.length; i++) {
        String result = separate[i].replace("(", "").replace(")", "");
     }
}  

While this does extract each integer, there is no distinction between the integers in brackets and the first one in the line. Also, this solution does not separate each line in the file.
Could someone please assist me with a better solution to this?

It looks like in your case it does not matter if the integers are in parenthesis or not. If so, you can do it this way.

String [] separate = line.replaceAll("[()\\s]+", "").split(",");

The replace above will replace multiple appearance of any of the symbols listed in [] with nothing. After it you would have a clean comma-separated string without parens or spaces.

separate[0] will point to the first element in the line.

I'd use regular expressions for this.

The pattern ^\\d+ will get you an integer at the start of the line.

The pattern ((?<=\\()\\d+)|(\\d+(?=\\))) will get you any integers directly preceded by or followed by parentheses. It looks scarier than it is. You can break it down like so:

(A)|(B)

Which is simply A or B.

A =   (?<=\()\d+

Which is one or more digits \\d+ preceded by a literal '(' (?<=\\() .

B =   \d+(?=\))

Which is one or more digits \\d+ followed by a literal ')' (?=\\))

Here's a little Java program which works on a single line. It could be easily adapted to work on all lines:

String line = "345 (1, 10), (0, 5), (3, 40)";

// Get the number at the start of the line:
final Matcher m = Pattern.compile("^\\d+").matcher(line);
if (m.find())
{
    System.out.println(m.group());
}

// Loop over any numbers in parentheses:
final Matcher mm = Pattern.compile("((?<=\\()\\d+)|(\\d+(?=\\)))").matcher(line);
boolean firstInPair = true;
while(mm.find())
{
    String name = firstInPair ? "First" : "Second";
    System.out.println(name + " " + mm.group());
    firstInPair = !firstInPair;
}

Sample output:

345
First 1
Second 10
First 0
Second 5
First 3
Second 40

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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