java 正则表达式删除 2 个字符串之间的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7660541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:52:18  来源:igfitidea点击:

regex remove everything between 2 strings

javaregex

提问by Decrypter

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

我的 replaceAll 需要一个正则表达式来删除 2 个字符串和字符串本身之间的所有内容。

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

我试图像这样 line.replaceAll("/n*/n", ""); 这应该导致

stackoverflow is really awesome I love it

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

我认为星号意味着什么,但不能让它工作?

cheers

干杯

回答by mb14

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

回答by Paul Cager

I think you need a dot:

我认为你需要一个点:

replaceAll("/n.*/n")

replaceAll("/n.*/n")

回答by Sorter

This will remove anything between ab.

这将删除ab.

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

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

请添加任何特殊字符,如果我遗漏了它。