Java 中的 Matcher quoteReplacement(String s) 方法示例

javaobject oriented programmingprogramming更新于 2024/5/8 22:36:17

Matcher 类的 appendReplacement() 方法接受 StringBuffer 对象和 String(替换字符串)作为参数,并将输入数据附加到 StringBuffer 对象,用替换字符串替换匹配的内容。

在内部,此方法从输入字符串中读取每个字符并附加到字符串缓冲区,每当发生匹配时,它都会将替换字符串(而不是匹配的内容部分)附加到缓冲区,并从匹配子字符串的下一个位置继续。

在将替换字符串传递给此方法时,如果您使用“/”或“$”,它们将不被视为常规字符,并会出现异常−

示例 1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacement {
   public static void main(String[] args) {
      String str = " <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>";
      //用于匹配粗体标签内容的正则表达式
      String regex = "<b>(\S+)</b>";
      System.out.println("输入字符串:\n"+str);
      //创建一个Pattern对象      模式模式 = Pattern.compile(regex);
      //匹配字符串中的编译模式      匹配器 matcher =pattern.matcher(str);
      //创建一个空的彩虹       StringBuffer sb = new StringBuffer();
      while (matcher.find()) {         matcher.appendReplacement(sb, "sampledata$" );
         //Matcher.quoteReplacement("eReplacement("eBo$ld/Data$"));
      }
      matcher.appendTail(sb);
      System.out.println("eStringBuffer 的内容:\n"e+ sb.toString() );
   }
}

输出

Input string:

<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>

Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference: group index is missing    at java.util.regex.Matcher.appendReplacement(Unknown Source)    at OCTOBER.matcher.QuoteReplacement.main(QuoteReplacement.java:18)

Matcher 类的引号替换方法接受一个字符串值并返回一个文字替换字符串。即,给定字符串中的字符 / 和 $ 将被忽略,并且结果可用作 appendReplacement() 方法的参数。

示例 2

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacement {
   public static void main(String[] args) {
      String str = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>";
      //Regular expression to match contents of the bold tags
      String regex = "<b>(\S+)</b>";
      System.out.println("输入字符串: \n"+str);
      //创建一个 Pattern 对象
      Pattern pattern = Pattern.compile(regex);
      //匹配字符串中的编译模式
      Matcher matcher = pattern.matcher(str);
      //创建一个空的字符串缓冲区
      StringBuffer sb = new StringBuffer();
      while (matcher.find()) {
         matcher.appendReplacement(sb, Matcher.quoteReplacement("Bo$ld/Data$"));
      }
      matcher.appendTail(sb);
      System.out.println("Contents of the StringBuffer: \n"+ sb.toString() );
   }
}

输出

输入字符串:
<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>
StringBuffer 的内容:
<p>此 Bo$ld/Data$ 是一个 Bo$ld/Data$ HTML Bo$ld/Data$。</p>

示例 3

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacementExample {
   public static void main(String[] args) {
      String input = "This is sample text";
      String regex = "[#]";
      //创建一个 Pattern 对象
      Pattern pattern = Pattern.compile(regex);
      //匹配字符串中的编译模式
      Matcher matcher = pattern.matcher(input);
      //创建一个空的字符串缓冲区
      String str = Matcher.quoteReplacement("sampledata");
      System.out.println(str);
   }
}

输出

sampledata

相关文章


有用资源