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

时间:2022-03-20 06:33:16

I needed to write a pattern searcher and replace matched pattern with *, I am able to do that, but the replaced star are of fixed size. I wanted that the replaced stars are of same length as matched pattern. is their any optimized way of doing that.

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

As in eg-

如在 - 例如 -

you to be replaced by ***

你被***取代

data by ****

数据按****

and

Us by **

我们的**

         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);
         }

Gives out put- Hai man how are **** ? Give me **** which **** have fetched as it is very important **** to ****

放弃了 - 海男怎么样****?给我****哪个****已经取得因为它非常重要****到****

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

我想 - 海男怎么样***?给我****哪个***已经取得因为它非常重要**** **

3 个解决方案

#1


2  

You can use the following approach: match what you need and use Matcher#appendReplacement to modify the matched substrings (to replace all chars in it with * that you say is a fixed masking char).

您可以使用以下方法:匹配您需要的内容并使用Matcher#appendReplacement修改匹配的子字符串(用*表示其中的所有字符替换为固定的屏蔽字符)。

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

See the online Java demo.

请参阅在线Java演示。

NOTE: If your string contains linebreaks, the replaceAll inside the while block must be changed to .replaceAll("(?s).", "*") to also replace linebreak chars with *.

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

#2


0  

Maybe the following codes are easy to be understood.

也许以下代码很容易理解。

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);

#3


0  

You can also achieve it in this way:

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

     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());

Hope this helps.

希望这可以帮助。

#1


2  

You can use the following approach: match what you need and use Matcher#appendReplacement to modify the matched substrings (to replace all chars in it with * that you say is a fixed masking char).

您可以使用以下方法:匹配您需要的内容并使用Matcher#appendReplacement修改匹配的子字符串(用*表示其中的所有字符替换为固定的屏蔽字符)。

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

See the online Java demo.

请参阅在线Java演示。

NOTE: If your string contains linebreaks, the replaceAll inside the while block must be changed to .replaceAll("(?s).", "*") to also replace linebreak chars with *.

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

#2


0  

Maybe the following codes are easy to be understood.

也许以下代码很容易理解。

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);

#3


0  

You can also achieve it in this way:

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

     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());

Hope this helps.

希望这可以帮助。