2015-06-26 20 views
1

在Java中,使用正则表达式仅在配对哈希标签中删除内容有更快的方法吗? 我会举一些例子:使用Java RegEx删除配对主题标签之间的内容##

When user logs in ## //Expected Result: When user logs in ## 
When user logs in # //Expected Result: When user logs in # 
When user logs in #the system# //Expected Result: When user logs in ## 
When user logs in #the system //Expected Result: When user logs in #the system 
When user logs in #the system# named #test //Expected Result: When user logs in ## named #test 

我见过的例子删除括号中的内容,但,这是因为一点点不同“(”和“)”是不同的字符,如果我只是使用replace,编译器不能到#the system##test

回答

1
之间

区分内容您可以使用:

str = str.replaceAll("(?!<#)#[^#]+#(?!#)", "##); 

RegEx Demo

+0

#。*?# 将是另一种选择! – Keews

+0

我在我的解决方案中使用了'#。+?#',而不是'#。*?#',因为OP希望保持###不变。 – anubhava

+0

你是完全正确的,我的错!抱歉。 – Keews

相关问题