Example string Пример пример №1 300986190 . How to get only the last numbers? It is necessary to get only the last digits even if there is such a time. Пример пример №2 1 2 3 4 5 6 6666666 .

I use the string like this String Tekst2 = "set UNP=" + ObyektProgramm2; where ObyektProgramm2 our string.

  • @MikhailVaysman The last word from the numbers, 6666666 - aaa
  • @MikhailVaysman The last word is the UNP, you need to consider it no matter what is ahead - aaa
  • @MikhailVaysman There is one exact sign, numbers - 9. I would like to search not by a space or comma, no matter what is written in front of all. Can there be an easy way to count the last 9 digits as just one line? - aaa
  • @Ele, in your second example the numbers are not nine - mymedia
  • @mymedia No matter, the question was how to get the last digits and not the last 9. I wrote 9 digits to make it possible to ease the decision. - aaa

1 answer 1

Regular expressions for extracting numbers at the end of a line are very simple: \d+$ . You have probably seen similar examples in your regular expression textbook. Just in case, below is an example.

 String s = "Пример №42"; Pattern p = Pattern.compile("\\d+$"); Matcher m = p.matcher(s); if (m.find()) { System.out.println(m.group()); } else { System.out.println("Цифр в конце нет"); } 

The backslash in the regular season is screened, so there are two of them.