简体   繁体   English

正则表达式删除两个字符串之间的所有内容,但重复

[英]Regex to remove everything between two strings but with repetitions

Example string: Before text, <ref>{{BLAHBLAHBLAHBLAH}}</ref> after text, <ref>{{reference2}}</ref> end of paragraph. 示例字符串: Before text, <ref>{{BLAHBLAHBLAHBLAH}}</ref> after text, <ref>{{reference2}}</ref> end of paragraph.

I'm looking to remove any mention of <ref> and </ref> and everything in between. 我希望删除所有<ref></ref>以及两者之间的所有内容。

The result that I want from above example string: Before text, after text, end of paragraph. 我要从上面的示例字符串中得到的结果: Before text, after text, end of paragraph.

I have read regex remove everything between 2 strings and have tried replaceAll("<ref>.*</ref>", "") , the problem I'm having is that the method in that thread is removing too much. 我读过regex删除了2个字符串之间的所有内容,并尝试了replaceAll("<ref>.*</ref>", "") ,我遇到的问题是该线程中的方法删除了太多内容。 If I use this method on the above text the result will be : Before text, end of paragraph . 如果我在上面的文本上使用此方法,则结果将是: Before text, end of paragraph It will cut out the text in-between the sets of <ref> tags. 它将剪切出<ref>标记集之间的文本。

Is there an easy regex way to achieve the desired result? 有没有简单的正则表达式方式来达到预期的效果? In my actual text there might be many (more than 2) sets of <ref> </ref> tags with useful text in-between. 在我的实际文本中,可能会有许多(超过2个) <ref> </ref>标签集,中间有有用的文本。

Edit: added the regex I tried. 编辑:添加了我尝试过的正则表达式。

I assume you tried it with replaceAll("<ref>.*</ref>", ""); 我假设您尝试使用replaceAll("<ref>.*</ref>", ""); . Try it with a questionmark, it makes the star non greedy. 尝试使用问号,它会使星形不贪心。 replaceAll("<ref>.*?</ref>", "");

If you're going off the answer in the referenced question you're indeed "removing too much" because the answer uses a greedy operator, use *? 如果您不在参考问题中回答,那么您确实在“删除过多”,因为答案使用贪婪的运算符,请使用*? for a non-greedy match instead ... 而不是贪婪的比赛...

I would propose the following regular expression if you must use one at all: 如果您必须使用一个正则表达式,我将提出以下正则表达式:

str = str.replaceAll("(?s)<ref>.*?</ref>", "");

Note: The inline (?s) mode modifier allows the dot to match across newline sequences. 注意:内联(?s)模式修饰符允许点在换行符序列之间匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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