Java Utililty Methods Regex String Replace First

List of utility methods to do Regex String Replace First

Description

The list of methods to do Regex String Replace First are organized into topic(s).

Method

StringreplaceFirst(final String text, final Pattern regex, final String replacement)

Replaces the first substring of the text string that matches the given regular expression pattern with the given replacement.

This method is a null safe equivalent to:
  • pattern.matcher(text).replaceFirst(replacement)

A null reference passed to this method is a no-op.

 StringUtils.replaceFirst(null, *, *)       = null StringUtils.replaceFirst("any", (Pattern) null, *)   = "any" StringUtils.replaceFirst("any", *, null)   = "any" StringUtils.replaceFirst("", Pattern.compile(""), "zzz")    = "zzz" StringUtils.replaceFirst("", Pattern.compile(".*"), "zzz")  = "zzz" StringUtils.replaceFirst("", Pattern.compile(".+"), "zzz")  = "" StringUtils.replaceFirst("abc", Pattern.compile(""), "ZZ")  = "ZZabc" StringUtils.replaceFirst("<__>\n<__>", Pattern.compile("<.*>"), "z")      = "z\n<__>" StringUtils.replaceFirst("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z")  = "z" StringUtils.replaceFirst("ABCabc123", Pattern.compile("[a-z]"), "_")          = "ABC_bc123" StringUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "_")  = "ABC_123abc" StringUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "")   = "ABC123abc" StringUtils.replaceFirst("Lorem ipsum  dolor   sit", Pattern.compile("( +)([a-z]+)"), "_$2")  = "Lorem_ipsum  dolor   sit" 
if (text == null || regex == null || replacement == null) {
    return text;
return regex.matcher(text).replaceFirst(replacement);
StringreplaceFirst(final String text, final String regex, final String replacement)

Replaces the first substring of the text string that matches the given regular expression with the given replacement.

This method is a null safe equivalent to:
  • text.replaceFirst(regex, replacement)
  • Pattern.compile(regex).matcher(text).replaceFirst(replacement)

A null reference passed to this method is a no-op.

The Pattern#DOTALL option is NOT automatically added.

if (text == null || regex == null || replacement == null) {
    return text;
return text.replaceFirst(regex, replacement);
StringreplaceFirst(String content, String regex, String replacement)
replace First
if (isNone(content)) {
    return content;
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
    content = matcher.replaceFirst(replacement);
return content;
StringreplaceFirst(String str, String regex, String replacement, int patternFlag)
replace First
return Pattern.compile(regex, patternFlag).matcher(str).replaceFirst(replacement);
booleanreplaceFirst(StringBuffer buf, StringBuffer aux, Pattern pattern, String replacement, int pos)
replace First
boolean chg = false;
aux.setLength(0);
Matcher matcher = pattern.matcher(buf);
if (matcher.find(pos)) {
    matcher.appendReplacement(aux, replacement);
    chg = true;
matcher.appendTail(aux);
...
StringreplaceFirstIgnoreCase(String source, String search, String replace)
replace First Ignore Case
source = (source == null) ? "" : source;
if (search == null) {
    return source;
replace = replace == null ? "" : replace;
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("((?i)");
strBuilder.append(Pattern.quote(search));
...
StringreplaceFirstOccurrence(String originalText, String oldString, String newString)
replace First Occurrence
Pattern pattern = Pattern.compile(oldString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(originalText);
StringBuffer sb = new StringBuffer();
matcher.reset();
if (matcher.find()) {
    matcher.appendReplacement(sb, newString);
matcher.appendTail(sb);
...
StringreplaceFirstStatement(String source, String regex, String statement)
replace First Statement
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern == null ? null : pattern.matcher(source);
return matcher == null ? null : matcher.replaceFirst(statement);
StringreplaceLeading(String string, char replaced, char replacedBy)
replace Leading
Pattern p = Pattern.compile(replaced + "++");
Matcher matcher = p.matcher(string);
if (matcher.find()) {
    string = matcher.replaceFirst(matcher.group(0).replace(replaced, replacedBy));
return string;