Java Matcher appendReplacement() method

This method is used to implement append-and-replace step. First, It replaces compiled character or word with given input sequence and then it appends the given replacement string to the string buffer.

Syntax

Parameter

strbuff - The target string buffer

replcmnt - The replacement string

Returns

This matcher

Throws

IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

IllegalArgumentException - If the replacement string refers to a named-capturing group that does not exist in the pattern

IndexOutOfBoundsException - If the replacement string refers to a capturing group that does not exist in the pattern

Example 1

Output:

'Let the cat
'Let the cat come out of the bag'.To disclose a secret(cat

Example 2

Output:

'Let the cat come out of the bag'.To disclose a secret(cat)**

Example 3

Output:

Start :  0 End : 3
Start :  8 End : 11
Start :  15 End : 19
-Hello-word-