Replace / delete a string between two characters

I want to delete a line that is between two characters, as well as the characters themselves, say, for example:

I want to replace the entire string between "#?" and ";" and delete it with characters.

From this

"this #?anystring; is  #?anystring2jk; test"

For this

"this is test"

how can i do this in java?

+4
source share
3 answers

Use regex:

myString.replaceAll("#\?.*?;", "");
+5
source

@computerish your answer is failing in Java. The modified version works.

myString.replaceAll("#\\?.*?;", "");

? , JVM escape-. ? , . () , .

+13

string.replaceAll(start+".*"+end, "")

is a simple starting point. You may have to deal with the greediness of regex operators.

+2
source

Source: https://habr.com/ru/post/1765736/


All Articles