Hello!

The input format of the string is: a different number of string (through spaces) space 3 int -a (through spaces). I need to break this line into two, in one string and in the second int .

I know that in Java there is a split method for this, but I do not know which regular expression to put in the parameters. Tell me how it can be done.

    1 answer 1

    I don’t know how to solve your problem using the split method, but it can be slightly different:

     Pattern p = Pattern.compile("([\\D]*) ([\\d]* [\\d]* [\\d]*)"); Matcher m = p.matcher(str); m.find(); String sStr = m.group(1); String iStr = m.group(2); 

    As a result, sStr is a string with words, iStr is a string with numbers.

    Just in case, you can check the result of the m.find() method, which returns true if a match is found and false otherwise.

    It can be even simpler - ([\D]*) ([\d ]*) .

    • Thanks for the answer. This is exactly what I needed. I don’t understand the essence of the regular expression a bit, but I’ll deal with it myself. - HatmZull
    • Prompt, in the resulting line will remain spaces? - HatmZull
    • one
      @HatmZull, Yes, they will. - post_zeew