當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.regex.Pattern.quote()用法及代碼示例


描述

這個java.util.regex.Pattern.quote(String s)方法返回指定字符串的文字模式字符串。

聲明

以下是聲明java.util.regex.Pattern.quote(String s)方法。

public static String quote(String s)

參數

  • s─ 要字麵化的字符串。

返回

文字字符串替換。

示例

下麵的例子展示了 java.util.regex.Pattern.quote(String s) 方法的用法。

package com.tutorialspoint;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternDemo {
   private static String REGEX = "dog$";
   private static String INPUT = "The dog$ says meow " + "All dog$ say meow.";
   private static String REPLACE = "cat";

   public static void main(String[] args) {
      Pattern pattern = Pattern.compile(Pattern.quote(REGEX));
      
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT); 
      INPUT = matcher.replaceAll(REPLACE);
      System.out.println(INPUT);
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

The cat says meow All cat say meow.

相關用法


注:本文由純淨天空篩選整理自 java.util.regex.Pattern.quote() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。