String s = "100.5 рублей."; System.out.println(s.replaceAll("^.*?(\\d+\\.\\d+).*$", "\\1"));
Regular expression:
^ - the beginning of the line, in this case is not necessary..*? - lazy selection of all characters (meaning that the dialect supports it)(\d+\.\d+) - select a number.* - select all characters left in the string$ - end of the line (also optional)
Expression to replace:
"\1" - replacing the string associated with the whole expression with the string associated with the first expression in brackets
It is more productive to use the
match('\d+\.\d+') method (matchSubstr, find or something else); The specifics depend on the language / library.
* The set of characters that need to be escaped (brackets, plus) depend on the dialect.
\\d+[.]\\d+, he deleted the price itself, I didn’t know where to write ^ for the whole pattern and turned the logic in every part of it - FlippyreplaceAll("^.*?\\(\\d+[.]\\d+\\).*$","\1")- Fat-Zer