A
A
askalidov2022-04-15 15:34:35
Java
askalidov, 2022-04-15 15:34:35

How to remove everything from a string except for a specific pattern?

There is a line:

"<какой-то текст или символы>130 кг<какой-то текст или символы>"

it is necessary to remove everything from the line, except for 130 kg.
In this case, instead of 130, there can be any number

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
avgwowenjoyer, 2022-04-15
@askalidov

It is possible to do this, provided that it is a single number with two or more signs:

String str = "<какой-то текст или символы>130 кг<какой-то текст или символы>";
String substr = str.replaceAll("\\d{2,}| кг", "");
String output= str.replaceAll(substr, "");
System.out.println(output);

A
Araya, 2022-04-15
@Araya

Regular season of course

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question