Decrypter
Decrypter

Reputation: 3000

regex remove everything between 2 strings

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

Upvotes: 4

Views: 17393

Answers (3)

Sorter
Sorter

Reputation: 10220

This will remove anything between ab.

replaceAll("ab([;\s\w\"\=\,\:\./\~\{\}\?\!\-\%\&\#\$\^\(\)]*?)ab",""); 

Please add any special character, if I'm missing it.

Upvotes: 2

mb14
mb14

Reputation: 22616

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 *", " ")

Upvotes: 11

Paul Cager
Paul Cager

Reputation: 1930

I think you need a dot:

replaceAll("/n.*/n")

Upvotes: 3

Related Questions