Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex remove everything between 2 strings

Tags:

java

regex

I need a regex for my replaceAll that removes everything between 2 strings and the strings themselves.

For example if I had something like.

stackoverflow is really awesome/nremove123/n I love it

I was trying to do a replaceAll like this line.replaceAll("/n*/n", ""); This should result in

stackoverflow is really awesome I love it

I thought the asterisk meant anything but can't get it to work?

cheers

like image 337
Decrypter Avatar asked Dec 07 '22 19:12

Decrypter


2 Answers

No, the . means any character. * means any number of the previous things. So anything is .*. What you need is

/n.*/n

If you want to leave an empty space between words use this instead

replaceAll("/n.*/n *", " ")
like image 130
mb14 Avatar answered Dec 29 '22 04:12

mb14


I think you need a dot:

replaceAll("/n.*/n")

like image 31
Paul Cager Avatar answered Dec 29 '22 04:12

Paul Cager