ReplaceAll匹配模式出现与星。等于匹配字符串的长度

时间:2016-10-13 06:03:36

标签: java regex string replace pattern-matching

我需要编写一个模式搜索器并用*替换匹配的模式,我能够做到这一点,但被替换的星是固定大小的。我希望被替换的恒星与匹配的模式具有相同的长度。是他们的任何优化方式。

与eg-

一样
  

你将被***替换

     <***>数据按****

     

     

我们**

         final String REGEX = "data|you|Us";
         final String MASK = "****";
         final Pattern PATTERN = Pattern.compile(REGEX);
         String message = "Hai man how are you ? Give me data which you have fetched as it is very important data to Us";
         Matcher matcher = PATTERN.matcher(message);        
         if (matcher.find()) {           
             String maskedMessage = matcher.replaceAll(MASK);             
             System.out.println(maskedMessage);
         }

放弃 - Hai man how are **** ? Give me **** which **** have fetched as it is very important **** to ****

我想要 - Hai man how are *** ? Give me **** which *** have fetched as it is very important **** to **

3 个答案:

答案 0 :(得分:2)

您可以使用以下方法:匹配您需要的内容并使用Matcher#appendReplacement修改匹配的子字符串(用*替换其中的所有字符,您说这是一个固定的屏蔽字符。)< / p>

final String REGEX = "data|you|Us";
final Pattern PATTERN = Pattern.compile(REGEX);
String message = "Hai man how are you ? Give me data which you have fetched as it is very important data to Us";
Matcher matcher = PATTERN.matcher(message);        
StringBuffer result = new StringBuffer();           // Buffer for the result
while (matcher.find()) {                            // Look for partial matches
    String replacement = 
        matcher.group(0).replaceAll(".", "*");      // Replace any char with `*`
    matcher.appendReplacement(result, replacement); // Append the modified string
}
matcher.appendTail(result); // Add the remaining string to the result
System.out.println(result.toString()); // Output the result

请参阅online Java demo

注意:如果您的字符串包含换行符,则replaceAll块中的while必须更改为.replaceAll("(?s).", "*"),以便将换行符替换为*

答案 1 :(得分:0)

以下代码可能很容易被理解。

final String REGEX = "data|you|Us";
final Pattern PATTERN = Pattern.compile(REGEX);
String message = "Hai man how are you ? Give me data which you have fetched as it is very important data to Us";
Matcher matcher = PATTERN.matcher(message);  
String maskedMessage ="";
while (matcher.find()) { 
   String rs = matcher.group();
   StringBuilder sb = new StringBuilder();
   for(int i = 0 ; i < rs.length(); i++){
       sb.append("*");
   }
   message = message.replace(rs, sb.toString());
   matcher = PATTERN.matcher(message);
}
maskedMessage = message;
System.out.println(maskedMessage);

答案 2 :(得分:0)

你也可以通过这种方式实现它:

     final String REGEX = "data|you|Us";
     final String MASK = "*";
     final Pattern PATTERN = Pattern.compile(REGEX);
     String message = "Hai man how are you ? Give me data which you have fetched as it is very important data to Us";
     StringBuilder messageBuilder = new StringBuilder(message);
     Matcher matcher = PATTERN.matcher(message);   

     int start=0;
     while (matcher.find(start)) {
         messageBuilder.replace(matcher.start(), matcher.end(), new String(new char[matcher.end()-matcher.start()]).replace("\0", MASK));
         start = matcher.end();
     }

     System.out.println(messageBuilder.toString());

希望这有帮助。