Splitting a string using Regex in Java

前端 未结 4 1029
别跟我提以往
别跟我提以往 2020-12-16 15:52

Would anyone be able to assist me with some regex.

I want to split the following string into a number, string number

\"810LN15\"

1 metho

4条回答
  •  醉梦人生
    2020-12-16 16:48

    In Java, as in most regex flavors (Python being a notable exception), the split() regex isn't required to consume any characters when it finds a match. Here I've used lookaheads and lookbehinds to match any position that has a digit one side of it and a non-digit on the other:

    String source = "810LN15";
    String[] parts = source.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
    System.out.println(Arrays.toString(parts));
    

    output:

    [810, LN, 15]
    

提交回复
热议问题