当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Matcher appendReplacement(StringBuilder, String)用法及代码示例


Matcher类的appendReplacement(StringBuilder,String)方法的行为与append-and-replace方法相同。此方法读取输入字符串,并将其替换为匹配字符串中的匹配模式。

用法:

public Matcher 
    appendReplacement(StringBuilder builder, 
                      String stringToBeReplaced)

参数:此方法有两个参数:


  • builder:这是存储目标字符串的StringBuilder。
  • stringToBeReplaced:这是匹配器中要替换的字符串。

返回值:此方法返回一个Matcher,其中替换了目标String。

异常:此方法引发以下异常:

  • IllegalStateException:如果尚未尝试匹配,或者先前的匹配操作失败
  • IllegalArgumentException:如果替换字符串指向模式中不存在的named-capturing组
  • IndexOutOfBoundsException:如果替换字符串指向模式中不存在的捕获组

下面的示例说明Matcher.appendReplacement()方法:

范例1:

// Java code to illustrate appendReplacement() method 
  
import java.util.regex.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the regex to be checked 
        String regex = "(Geeks)"; 
  
        // Create a pattern from regex 
        Pattern pattern 
            = Pattern.compile(regex); 
  
        // Get the String to be matched 
        String stringToBeMatched 
            = "GeeksForGeeks Geeks for For Geeks Geek"; 
  
        // Create a matcher for the input String 
        Matcher matcher 
            = pattern.matcher(stringToBeMatched); 
  
        System.out.println("Before Replacement:"
                           + stringToBeMatched); 
  
        // Get the String to be replaced 
        String stringToBeReplaced = "GEEKS"; 
        StringBuilder builder = new StringBuilder(); 
  
        // Replace every matched pattern 
        // with the target String 
        // using appendReplacement() method 
        while (matcher.find()) { 
            matcher.appendReplacement(builder, 
                                      stringToBeReplaced); 
        } 
        matcher.appendTail(builder); 
  
        // Print the replaced matcher 
        System.out.println("After Replacement:"
                           + builder.toString()); 
    } 
}
输出:
Before Replacement:GeeksForGeeks Geeks for For Geeks Geek
After Replacement:GEEKSForGEEKS GEEKS for For GEEKS Geek

范例2:

// Java code to illustrate appendReplacement() method 
  
import java.util.regex.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the regex to be checked 
        String regex = "(FGF)"; 
  
        // Create a pattern from regex 
        Pattern pattern 
            = Pattern.compile(regex); 
  
        // Get the String to be matched 
        String stringToBeMatched 
            = "FGF FGF FGF FGF"; 
  
        // Create a matcher for the input String 
        Matcher matcher 
            = pattern.matcher(stringToBeMatched); 
  
        System.out.println("Before Replacement:"
                           + stringToBeMatched); 
  
        // Get the String to be replaced 
        String stringToBeReplaced = "GFG"; 
        StringBuilder builder = new StringBuilder(); 
  
        // Replace every matched pattern 
        // with the target String 
        // using appendReplacement() method 
        while (matcher.find()) { 
            matcher.appendReplacement(builder, 
                                      stringToBeReplaced); 
        } 
        matcher.appendTail(builder); 
  
        // Print the replaced matcher 
        System.out.println("After Replacement:"
                           + builder.toString()); 
    } 
}
输出:
Before Replacement:FGF FGF FGF FGF
After Replacement:GFG GFG GFG GFG

参考: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#appendReplacement-java.lang.StringBuilder-java.lang.String-



相关用法


注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 Matcher appendReplacement(StringBuilder, String) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。