java.util.regex.Matcher.quoteReplacement ()
Descrizione
Il java.time.Matcher.quoteReplacement(String s) restituisce una stringa di sostituzione letterale per la stringa specificata.
Dichiarazione
Di seguito è riportata la dichiarazione per java.time.Matcher.quoteReplacement(String s) metodo.
public static String quoteReplacement(String s)
Parametri
s - La stringa da letteralizzare.
Valore di ritorno
Una sostituzione letterale di stringa.
Esempio
L'esempio seguente mostra l'utilizzo del metodo java.time.Matcher.quoteReplacement (String s).
package com.tutorialspoint;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherDemo {
private static String REGEX = "dog";
private static String INPUT = "The dog says meow " + "All dogs say meow.";
private static String REPLACE = "cat$";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
// get a matcher object
Matcher matcher = pattern.matcher(INPUT);
try{
//Below line will throw exception
INPUT = matcher.replaceAll(REPLACE);
} catch(Exception e){
System.out.println("Exception: "+ e.getMessage());
}
INPUT = matcher.replaceAll(matcher.quoteReplacement(REPLACE));
System.out.println(INPUT);
}
}
Compiliamo ed eseguiamo il programma sopra, questo produrrà il seguente risultato:
Exception: Illegal group reference: group index is missing
The cat$ says meow All cat$s say meow.