Substitute each instance of a regular expression match with a designated substring.
String myStr = “I love cats. Cats are very easy to love. Cats are very popular.”; ionMatches(int offset, String other, int otherOffset, int length) |
The replaceAll() method in Java replaces every match of a regular expression within a string with a new substring.
Replacement strings may include a backreference denoted as $n, where n represents the index of a group in the pattern. Within the resulting string, occurrences of $n will be substituted with the substring matched by the corresponding group. If $0 is employed, it will be substituted with the entire expression. Refer to “More Examples” below for an illustration of using a backreference.
Suggestion: Consult the Java RegEx tutorial to gain insights into regular expressions.
public |
Parameter |
Description |
regex |
Mandatory. A regular expression that specifies the substrings to be searched for. |
replacement |
Necessary. The substring that will substitute each match. |
REturns |
A duplicate of the string where matches of the regular expression have been substituted with new substrings. |
Throws |
PatternSyntaxException occurs if the syntax of the regular expression is incorrect. |
Java Version |
1.4 |
Utilize a backreference to enclose numbers within parentheses.
String |