Regular Expression Matching -Java

后端 未结 6 1723
轻奢々
轻奢々 2021-01-23 20:48

I am taking input from a file in following format:

(int1,int2) (int3,int4)

Now I want to read int1, int2, int3 and int4 in my Java code. How ca

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-23 20:50

    Pattern p = Pattern.compile("\\((\\d+),(\\d+)\\)\\s+\\((\\d+),(\\d+)\\)");
    String input = "(123,456) (789,012)";
    
    Matcher m = p.matcher(input);
    
    if (m.matches()) {
      int a = Integer.parseInt(m.group(1), 10);
      int b = Integer.parseInt(m.group(2), 10);
      int c = Integer.parseInt(m.group(3), 10);
      int d = Integer.parseInt(m.group(4), 10);
    }
    

提交回复
热议问题