Splitting a string using Regex in Java

前端 未结 4 1030
别跟我提以往
别跟我提以往 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:31

    This gives you the exact thing you guys are looking for

            Pattern p = Pattern.compile("(([a-zA-Z]+)|(\\d+))|((\\d+)|([a-zA-Z]+))");
            Matcher m = p.matcher("810LN15");
            List<Object> tokens = new LinkedList<Object>();
            while(m.find())
            {
              String token = m.group( 1 ); 
              tokens.add(token);
            }
            System.out.println(tokens);
    
    0 讨论(0)
  • 2020-12-16 16:33

    (\\d+)([a-zA-Z]+)(\\d+) should do the trick. The first capture group will be the first number, the second capture group will be the letters in between and the third capture group will be the second number. The double backslashes are for java.

    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 2020-12-16 16:49

    String.split won't give you the desired result, which I guess would be "810", "LN", "15", since it would have to look for a token to split at and would strip that token.

    Try Pattern and Matcher instead, using this regex: (\d+)|([a-zA-Z]+), which would match any sequence of numbers and letters and get distinct number/text groups (i.e. "AA810LN15QQ12345" would result in the groups "AA", "810", "LN", "15", "QQ" and "12345").

    Example:

    Pattern p = Pattern.compile("(\\d+)|([a-zA-Z]+)");
    Matcher m = p.matcher("810LN15");
    List<String> tokens = new LinkedList<String>();
    while(m.find())
    {
      String token = m.group( 1 ); //group 0 is always the entire match   
      tokens.add(token);
    }
    //now iterate through 'tokens' and check whether you have a number or text
    
    0 讨论(0)
提交回复
热议问题