简体   繁体   中英

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

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

I think you need a dot:

replaceAll("/n.*/n")

This will remove anything between ab .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM