[英]Replacing group of characters using regular expression in Java
I have a string like this: 我有一个像这样的字符串:
<b.*?n.*? .*?a.*?n.*? .*?e.*? .*? .*? .*? .e.*?t.*?e.*? .*? .*? .*? .*?<.r>.*? .*?e.*? .*? .*? .*?l.*?e .*?b.*?e.*?n.*?b.*? .*? .*?a.*?p.*?t.*? .*?l.*? .*? .*? .*? .*? ..b.*? .*? .*?s.*? .*?s.*? .*?>.*? .*?es.*? .*?n.*?t.*?r.*? .*? .*? .*? .n.*?b.*? .*? .*? .*? .*? .*?e.*? .*? .*? .*? .*?t.*?n.*? .*? .*? .*? .*?n.s.. .*?a. .*? <.*?e.e.*? comp.*? .*?<.*? .*?r.*?el.*?&.*?b.*? .*? .*?y.*? ..e.*? .. .*? .*? .*? .*? .*?u.t.*?m..o.*?b.*?r.*?e.*?n.*?o.. .*? .*?s. .*?e.*?o.. .*?r.*?r.*?&.*?b.*? ..b>.*?o.*?s.*? .*?r.*? .*? .*?a..i.*? .*? .*? .*?e.*?in.*? .*? .*? .*? .*? .*? .*? ..b.*? .*? .*? .*?r..i.*?c.*? .*? .*?o.*?<..>.*?
Sorry but the *
is removed from the text above. 抱歉,
*
已从上面的文本中删除。
I want to be able to replace consecutive appearance of .*?
我希望能够替换连续出现的
.*?
into a single .*?
成一个
.*?
. 。 I tried
我试过了
res = tmp.replaceAll("(\\s\\.\\*\\?)(\\1{2,})","\\s\\.\\*\\?");
but it did not work. 但它没有用。 Is there something wrong in my code?
我的代码有问题吗?
I would suggest you to use loop and replace it. 我建议您使用循环并替换它。 something like follow
像跟随
String pattern= "(.\*\?\s.\*\?)";
if(YOURSTRING.matches(pattern)){
// replace two .*? to one
}
else{
// break your loop
}
this is not a complete solution. 这不是一个完整的解决方案。 But some basic idea
但是一些基本的想法
You could try this: 您可以尝试以下方法:
str.replaceAll("\\p{Blank}+", "").replaceAll("[\\.\\*\\?]+", ".*?");
Example: 例:
String str = "abcabcddabceeabcabc";
str = str.replaceAll("[abc]+", "abc");
System.out.println("str = " + str);
And the result: 结果:
str = abcddabceeabc
You can use 您可以使用
(\s*)(?:\s*\.\*\?){2,}
And replace with $1.*?
并替换为
$1.*?
. 。 The Group 1 backreference
$1
will keep the initial whitespace before the first .*?
组1后向引用
$1
将在第一个.*?
之前保留初始空白.*?
. 。
The regex matches: 正则表达式匹配:
(\\s*)
- Group 1: zero or more whitespace symbols (\\s*)
-第1组:零个或多个空格符号 (?:\\s*\\.\\*\\?){2,}
- two or more sequences of: (?:\\s*\\.\\*\\?){2,}
-两个或更多序列:
\\s*
- zero or more whitespace symbols \\s*
-零个或多个空格符号 \\.
- a literal dot \\*
- a literal asterisk \\*
-文字星号 \\?
- a literal ?
?
symbol See the regex demo . 参见regex演示 。
Sample code: 样例代码:
String p = "(\\s*)(?:\\s*\\.\\*\\?){2,}";
String str = "<b.*?n.*? .*?a.*?n.*? .*?e.*? \\.*? .*? .*? .e.*?t.*?e.*? .*? .*? .*? .*?<.r>.*? .*?e.*? .*? .*? .*?l.*?e .*?b.*?e.*?n.*?b.*? .*? .*?a.*?p.*?t.*? .*?l.*? .*? .*? .*? .*? ..b.*? .*? .*?s.*? .*?s.*? .*?>.*? .*?es.*? .*?n.*?t.*?r.*? .*? .*? .*? .n.*?b.*? .*? .*? .*? .*? .*?e.*? .*? .*? .*? .*?t.*?n.*? .*? .*? .*? .*?n.s.. .*?a. .*? <.*?e.e.*? comp.*? .*?<.*? .*?r.*?el.*?&.*?b.*? .*? .*?y.*? ..e.*? .. .*? .*? .*? .*? .*?u.t.*?m..o.*?b.*?r.*?e.*?n.*?o.. .*? .*?s. .*?e.*?o.. .*?r.*?r.*?&.*?b.*? ..b>.*?o.*?s.*? .*?r.*? .*? .*?a..i.*? .*? .*? .*?e.*?in.*? .*? .*? .*? .*? .*? .*? ..b.*? .*? .*? .*?r..i.*?c.*? .*? .*?o.*?<..>.*?";
String subst = "$1.*?";
System.out.println(str.replaceAll(p, subst));
See IDEONE demo 见IDEONE演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.