There is a text file that I want to parse, namely, read the data line but exclude several elements from it.

String examples:

бла-бла-бла 12312312 бла-бла-бла-бла983-бла {1} {12 000 123,09} бла-бла-бла 123123123123 {3} {020 123,09} бла-бла-бла 12312312934393123 бла93-бла0-бла {123,09} 

it is necessary when reading the string to get rid of the numbers in curly brackets. If that put braces for selection, in fact they are not. How to exclude them? I tried to split regular expressions with something like:

 String b = "строки"; String[] a = b.trim().split("^(?!0.*$)([0-9]{1,3}( [0-9]{3})?( [0-9]{3})?( [0-9]{3})?(,[0-9]{2})?)$") 

But I suspect that I am doing wrong. Please help by example.

  • What is in b ? One line of type бла-бла-бла 123123123123 {3} {020 123,09} ? Are numbers always in curly braces at the end of a line? Try String result = b.replaceAll("(?:\\s*\\{[^{}]*})+\\s*$", ""); . - Wiktor Stribiżew
  • Yes, the numbers are at the end of the line. I'm going to try now. - Philipesko
  • Unfortunately it did not help, the numbers are hanging. - Philipesko
  • In b, there is one of the lines, I didn’t begin to write all the code, I parse the lines through for. - Philipesko
  • Strange, it works here . - Wiktor Stribiżew

1 answer 1

Try a regular expression (?:[\s,]\d{1,3})+$ :

 b.replace("(?:[\\s,]\\d{1,3})+$", ""); 

Let's see what happens:

 (?: // Выделяем группу для повтора, но без захвата [\s,] // Пробельный символ или запятая \d{1,3} // От одной до трёх цифр )+ // Эта комбинация может повторяться $ // Всё это только в конце строки 

Regex101.com

  • Great, thanks for the explanation. It works just as well! - Philipesko
  • Is there any literature or a useful reference for regular expressions to pull up the knowledge? Thank. - Philipesko
  • I don’t know about references, use more - you understand more - vp_arth