Regular Expression Matching -Java

后端 未结 6 1658
轻奢々
轻奢々 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:59

    String[] ints = "(2,3) (4,5)".split("\\D+");
    System.out.println(Arrays.asList(ints));
    // prints [, 2, 3, 4, 5]
    

    To avoid empty values:

    String[] ints = "(2,3) (4,5)".replaceAll("^\\D*(.*)\\D*$", "$1").split("\\D+");
    System.out.println(Arrays.asList(ints));
    // prints [2, 3, 4, 5]
    

提交回复
热议问题