Top Description Fields Constructors Methods
org.apache.commons.lang3

public Class RegExUtils

extends Object
Class Inheritance
Imports
java.util.Objects, java.util.regex.Matcher, .Pattern

Helpers to process Strings using regular expressions.
Since
3.8
See Also
java.util.regex.Pattern

Field Summary

Modifier and TypeField and Description
pack-priv static final Pattern
VERSION_SPLIT_PATTERN

The pattern to split version strings.

Constructor Summary

AccessConstructor and Description
public
RegExUtils()
Deprecated TODO Make private in 4.0.

Make private in 4.0.

Method Summary

Modifier and TypeMethod and Description
public static Pattern

Returns:

the given regular expression compiled into a pattern with the Pattern#DOTALL flag.
dotAll
(final String
The expression to be compiled.
regex
)

Compiles the given regular expression into a pattern with the Pattern#DOTALL flag.

public static Matcher

Returns:

A new matcher for this pattern.
dotAllMatcher
(final String
The expression to be compiled.
regex
,
final CharSequence
The character sequence to be matched.
text
)

Compiles the given regular expression into a pattern with the Pattern#DOTALL flag, then creates a matcher that will match the given text against this pattern.

public static Matcher

Returns:

A new matcher for this pattern.
dotAllMatcher
(final String
The expression to be compiled.
regex
,
final String
The character sequence to be matched.
text
)

Compiles the given regular expression into a pattern with the Pattern#DOTALL flag, then creates a matcher that will match the given text against this pattern.

public static String

Returns:

the text with any removes processed, null if null String input.
removeAll
(final CharSequence
text to remove from, may be null.
text
,
final Pattern
the regular expression to which this string is to be matched.
regex
)

Removes each substring of the text String that matches the given regular expression pattern.

public static String

Returns:

the text with any removes processed, null if null String input.
removeAll
(final String
text to remove from, may be null.
text
,
final Pattern
the regular expression to which this string is to be matched
regex
)

Removes each substring of the text String that matches the given regular expression pattern.

public static String

Returns:

the text with any removes processed, null if null String input.
removeAll
(final String
text to remove from, may be null
text
,
final String
the regular expression to which this string is to be matched
regex
)

Removes each substring of the text String that matches the given regular expression.

public static String

Returns:

the text with the first replacement processed, null if null String input.
removeFirst
(final CharSequence
text to remove from, may be null.
text
,
final Pattern
the regular expression pattern to which this string is to be matched.
regex
)

Removes the first substring of the text string that matches the given regular expression pattern.

public static String

Returns:

the text with the first replacement processed, null if null String input.
removeFirst
(final String
text to remove from, may be null.
text
,
final Pattern
the regular expression pattern to which this string is to be matched.
regex
)

Removes the first substring of the text string that matches the given regular expression pattern.

public static String

Returns:

the text with the first replacement processed, null if null String input.
removeFirst
(final String
text to remove from, may be null.
text
,
final String
the regular expression to which this string is to be matched.
regex
)

Removes the first substring of the text string that matches the given regular expression.

public static String

Returns:

The resulting String.
removePattern
(final CharSequence
the source string.
text
,
final String
the regular expression to which this string is to be matched.
regex
)

Removes each substring of the source String that matches the given regular expression using the DOTALL option.

public static String

Returns:

The resulting String.
removePattern
(final String
the source string.
text
,
final String
the regular expression to which this string is to be matched.
regex
)

Removes each substring of the source String that matches the given regular expression using the DOTALL option.

public static String

Returns:

the text with any replacements processed, null if null String input.
replaceAll
(final CharSequence
text to search and replace in, may be null.
text
,
final Pattern
the regular expression pattern to which this string is to be matched.
regex
,
final String
the string to be substituted for each match.
replacement
)

Replaces each substring of the text String that matches the given regular expression pattern with the given replacement.

public static String

Returns:

the text with any replacements processed, null if null String input.
replaceAll
(final String
text to search and replace in, may be null.
text
,
final Pattern
the regular expression pattern to which this string is to be matched.
regex
,
final String
the string to be substituted for each match.
replacement
)

Replaces each substring of the text String that matches the given regular expression pattern with the given replacement.

public static String

Returns:

the text with any replacements processed, null if null String input.
replaceAll
(final String
text to search and replace in, may be null.
text
,
final String
the regular expression to which this string is to be matched.
regex
,
final String
the string to be substituted for each match.
replacement
)

Replaces each substring of the text String that matches the given regular expression with the given replacement.

public static String

Returns:

the text with the first replacement processed, null if null String input.
replaceFirst
(final CharSequence
text to search and replace in, may be null.
text
,
final Pattern
the regular expression pattern to which this string is to be matched.
regex
,
final String
the string to be substituted for the first match
replacement
)

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

public static String

Returns:

the text with the first replacement processed, null if null String input.
replaceFirst
(final String
text to search and replace in, may be null.
text
,
final Pattern
the regular expression pattern to which this string is to be matched.
regex
,
final String
the string to be substituted for the first match.
replacement
)

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

public static String

Returns:

the text with the first replacement processed, null if null String input.
replaceFirst
(final String
text to search and replace in, may be null.
text
,
final String
the regular expression to which this string is to be matched.
regex
,
final String
the string to be substituted for the first match.
replacement
)

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

public static String

Returns:

The resulting String.
replacePattern
(final CharSequence
the source string.
text
,
final String
the regular expression to which this string is to be matched.
regex
,
final String
the string to be substituted for each match.
replacement
)

Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern#DOTALL option.

public static String

Returns:

The resulting String.
replacePattern
(final String
the source string.
text
,
final String
the regular expression to which this string is to be matched.
regex
,
final String
the string to be substituted for each match.
replacement
)

Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern#DOTALL option.

private static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

VERSION_SPLIT_PATTERNback to summary
pack-priv static final Pattern VERSION_SPLIT_PATTERN

The pattern to split version strings.

Constructor Detail

RegExUtilsback to summary
public RegExUtils()

Deprecated

TODO Make private in 4.0.

Make private in 4.0.

Annotations
@Deprecated

Method Detail

dotAllback to summary
public static Pattern dotAll(final String regex)

Compiles the given regular expression into a pattern with the Pattern#DOTALL flag.

Parameters
regex:String

The expression to be compiled.

Returns:Pattern

the given regular expression compiled into a pattern with the Pattern#DOTALL flag.

Since
3.13.0
dotAllMatcherback to summary
public static Matcher dotAllMatcher(final String regex, final CharSequence text)

Compiles the given regular expression into a pattern with the Pattern#DOTALL flag, then creates a matcher that will match the given text against this pattern.

Parameters
regex:String

The expression to be compiled.

text:CharSequence

The character sequence to be matched.

Returns:Matcher

A new matcher for this pattern.

Since
3.18.0
dotAllMatcherback to summary
public static Matcher dotAllMatcher(final String regex, final String text)

Compiles the given regular expression into a pattern with the Pattern#DOTALL flag, then creates a matcher that will match the given text against this pattern.

Parameters
regex:String

The expression to be compiled.

text:String

The character sequence to be matched.

Returns:Matcher

A new matcher for this pattern.

Annotations
@Deprecated
Since
3.13.0
removeAllback to summary
public static String removeAll(final CharSequence text, final Pattern regex)

Removes each substring of the text String that matches the given regular expression pattern. This method is a null safe equivalent to:

  • pattern.matcher(text).replaceAll(StringUtils.EMPTY)

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

StringUtils.removeAll(null, *)      = null
StringUtils.removeAll("any", (Pattern) null)  = "any"
StringUtils.removeAll("any", Pattern.compile(""))    = "any"
StringUtils.removeAll("any", Pattern.compile(".*"))  = ""
StringUtils.removeAll("any", Pattern.compile(".+"))  = ""
StringUtils.removeAll("abc", Pattern.compile(".?"))  = ""
StringUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>"))      = "A\nB"
StringUtils.removeAll("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))  = "AB"
StringUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>", Pattern.DOTALL))  = "AB"
StringUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]"))     = "ABC123"
Parameters
text:CharSequence

text to remove from, may be null.

regex:Pattern

the regular expression to which this string is to be matched.

Returns:String

the text with any removes processed, null if null String input.

Since
3.18.0
See Also
replaceAll(CharSequence, Pattern, String), java.util.regex.Matcher#replaceAll(String), java.util.regex.Pattern
removeAllback to summary
public static String removeAll(final String text, final Pattern regex)

Removes each substring of the text String that matches the given regular expression pattern. This method is a null safe equivalent to:

  • pattern.matcher(text).replaceAll(StringUtils.EMPTY)

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

StringUtils.removeAll(null, *)      = null
StringUtils.removeAll("any", (Pattern) null)  = "any"
StringUtils.removeAll("any", Pattern.compile(""))    = "any"
StringUtils.removeAll("any", Pattern.compile(".*"))  = ""
StringUtils.removeAll("any", Pattern.compile(".+"))  = ""
StringUtils.removeAll("abc", Pattern.compile(".?"))  = ""
StringUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>"))      = "A\nB"
StringUtils.removeAll("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))  = "AB"
StringUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>", Pattern.DOTALL))  = "AB"
StringUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]"))     = "ABC123"
Parameters
text:String

text to remove from, may be null.

regex:Pattern

the regular expression to which this string is to be matched

Returns:String

the text with any removes processed, null if null String input.

Annotations
@Deprecated
See Also
replaceAll(CharSequence, Pattern, String), java.util.regex.Matcher#replaceAll(String), java.util.regex.Pattern
removeAllback to summary
public static String removeAll(final String text, final String regex)

Removes each substring of the text String that matches the given regular expression. This method is a null safe equivalent to:

  • text.replaceAll(regex, StringUtils.EMPTY)
  • Pattern.compile(regex).matcher(text).replaceAll(StringUtils.EMPTY)

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

Unlike in the removePattern(CharSequence, String) method, the Pattern#DOTALL option is NOT automatically added. To use the DOTALL option prepend "(?s)" to the regex. DOTALL is also known as single-line mode in Perl.

StringUtils.removeAll(null, *)      = null
StringUtils.removeAll("any", (String) null)  = "any"
StringUtils.removeAll("any", "")    = "any"
StringUtils.removeAll("any", ".*")  = ""
StringUtils.removeAll("any", ".+")  = ""
StringUtils.removeAll("abc", ".?")  = ""
StringUtils.removeAll("A<__>\n<__>B", "<.*>")      = "A\nB"
StringUtils.removeAll("A<__>\n<__>B", "(?s)<.*>")  = "AB"
StringUtils.removeAll("ABCabc123abc", "[a-z]")     = "ABC123"
Parameters
text:String

text to remove from, may be null

regex:String

the regular expression to which this string is to be matched

Returns:String

the text with any removes processed, null if null String input.

Exceptions
PatternSyntaxException:
if the regular expression's syntax is invalid.
See Also
replaceAll(String, String, String), removePattern(CharSequence, String), String#replaceAll(String, String), java.util.regex.Pattern, java.util.regex.Pattern#DOTALL
removeFirstback to summary
public static String removeFirst(final CharSequence text, final Pattern regex)

Removes the first substring of the text string that matches the given regular expression pattern. This method is a null safe equivalent to:

  • pattern.matcher(text).replaceFirst(StringUtils.EMPTY)

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

StringUtils.removeFirst(null, *)      = null
StringUtils.removeFirst("any", (Pattern) null)  = "any"
StringUtils.removeFirst("any", Pattern.compile(""))    = "any"
StringUtils.removeFirst("any", Pattern.compile(".*"))  = ""
StringUtils.removeFirst("any", Pattern.compile(".+"))  = ""
StringUtils.removeFirst("abc", Pattern.compile(".?"))  = "bc"
StringUtils.removeFirst("A<__>\n<__>B", Pattern.compile("<.*>"))      = "A\n<__>B"
StringUtils.removeFirst("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))  = "AB"
StringUtils.removeFirst("ABCabc123", Pattern.compile("[a-z]"))          = "ABCbc123"
StringUtils.removeFirst("ABCabc123abc", Pattern.compile("[a-z]+"))      = "ABC123abc"
Parameters
text:CharSequence

text to remove from, may be null.

regex:Pattern

the regular expression pattern to which this string is to be matched.

Returns:String

the text with the first replacement processed, null if null String input.

Since
3.18.0
See Also
replaceFirst(String, Pattern, String), java.util.regex.Matcher#replaceFirst(String), java.util.regex.Pattern
removeFirstback to summary
public static String removeFirst(final String text, final Pattern regex)

Removes the first substring of the text string that matches the given regular expression pattern. This method is a null safe equivalent to:

  • pattern.matcher(text).replaceFirst(StringUtils.EMPTY)

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

StringUtils.removeFirst(null, *)      = null
StringUtils.removeFirst("any", (Pattern) null)  = "any"
StringUtils.removeFirst("any", Pattern.compile(""))    = "any"
StringUtils.removeFirst("any", Pattern.compile(".*"))  = ""
StringUtils.removeFirst("any", Pattern.compile(".+"))  = ""
StringUtils.removeFirst("abc", Pattern.compile(".?"))  = "bc"
StringUtils.removeFirst("A<__>\n<__>B", Pattern.compile("<.*>"))      = "A\n<__>B"
StringUtils.removeFirst("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))  = "AB"
StringUtils.removeFirst("ABCabc123", Pattern.compile("[a-z]"))          = "ABCbc123"
StringUtils.removeFirst("ABCabc123abc", Pattern.compile("[a-z]+"))      = "ABC123abc"
Parameters
text:String

text to remove from, may be null.

regex:Pattern

the regular expression pattern to which this string is to be matched.

Returns:String

the text with the first replacement processed, null if null String input.

Annotations
@Deprecated
See Also
replaceFirst(String, Pattern, String), java.util.regex.Matcher#replaceFirst(String), java.util.regex.Pattern
removeFirstback to summary
public static String removeFirst(final String text, final String regex)

Removes the first substring of the text string that matches the given regular expression. This method is a null safe equivalent to:

  • text.replaceFirst(regex, StringUtils.EMPTY)
  • Pattern.compile(regex).matcher(text).replaceFirst(StringUtils.EMPTY)

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

The Pattern#DOTALL option is NOT automatically added. To use the DOTALL option prepend "(?s)" to the regex. DOTALL is also known as single-line mode in Perl.

StringUtils.removeFirst(null, *)      = null
StringUtils.removeFirst("any", (String) null)  = "any"
StringUtils.removeFirst("any", "")    = "any"
StringUtils.removeFirst("any", ".*")  = ""
StringUtils.removeFirst("any", ".+")  = ""
StringUtils.removeFirst("abc", ".?")  = "bc"
StringUtils.removeFirst("A<__>\n<__>B", "<.*>")      = "A\n<__>B"
StringUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>")  = "AB"
StringUtils.removeFirst("ABCabc123", "[a-z]")          = "ABCbc123"
StringUtils.removeFirst("ABCabc123abc", "[a-z]+")      = "ABC123abc"
Parameters
text:String

text to remove from, may be null.

regex:String

the regular expression to which this string is to be matched.

Returns:String

the text with the first replacement processed, null if null String input.

Exceptions
PatternSyntaxException:
if the regular expression's syntax is invalid.
See Also
replaceFirst(String, String, String), String#replaceFirst(String, String), java.util.regex.Pattern, java.util.regex.Pattern#DOTALL
removePatternback to summary
public static String removePattern(final CharSequence text, final String regex)

Removes each substring of the source String that matches the given regular expression using the DOTALL option. This call is a null safe equivalent to:

  • text.replaceAll("(?s)" + regex, StringUtils.EMPTY)
  • Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(StringUtils.EMPTY)

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

StringUtils.removePattern(null, *)       = null
StringUtils.removePattern("any", (String) null)   = "any"
StringUtils.removePattern("A<__>\n<__>B", "<.*>")  = "AB"
StringUtils.removePattern("ABCabc123", "[a-z]")    = "ABC123"
Parameters
text:CharSequence

the source string.

regex:String

the regular expression to which this string is to be matched.

Returns:String

The resulting String.

Since
3.18.0
See Also
replacePattern(CharSequence, String, String), String#replaceAll(String, String), Pattern#DOTALL
removePatternback to summary
public static String removePattern(final String text, final String regex)

Removes each substring of the source String that matches the given regular expression using the DOTALL option. This call is a null safe equivalent to:

  • text.replaceAll("(?s)" + regex, StringUtils.EMPTY)
  • Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(StringUtils.EMPTY)

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

StringUtils.removePattern(null, *)       = null
StringUtils.removePattern("any", (String) null)   = "any"
StringUtils.removePattern("A<__>\n<__>B", "<.*>")  = "AB"
StringUtils.removePattern("ABCabc123", "[a-z]")    = "ABC123"
Parameters
text:String

the source string.

regex:String

the regular expression to which this string is to be matched.

Returns:String

The resulting String.

Annotations
@Deprecated
See Also
replacePattern(CharSequence, String, String), String#replaceAll(String, String), Pattern#DOTALL
replaceAllback to summary
public static String replaceAll(final CharSequence text, final Pattern regex, final String replacement)

Replaces each 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).replaceAll(replacement)

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

StringUtils.replaceAll(null, *, *)       = null
StringUtils.replaceAll("any", (Pattern) null, *)   = "any"
StringUtils.replaceAll("any", *, null)   = "any"
StringUtils.replaceAll("", Pattern.compile(""), "zzz")    = "zzz"
StringUtils.replaceAll("", Pattern.compile(".*"), "zzz")  = "zzz"
StringUtils.replaceAll("", Pattern.compile(".+"), "zzz")  = ""
StringUtils.replaceAll("abc", Pattern.compile(""), "ZZ")  = "ZZaZZbZZcZZ"
StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>"), "z")                 = "z\nz"
StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>", Pattern.DOTALL), "z") = "z"
StringUtils.replaceAll("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z")             = "z"
StringUtils.replaceAll("ABCabc123", Pattern.compile("[a-z]"), "_")       = "ABC___123"
StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "_")  = "ABC_123"
StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "")   = "ABC123"
StringUtils.replaceAll("Lorem ipsum  dolor   sit", Pattern.compile("( +)([a-z]+)"), "_$2")  = "Lorem_ipsum_dolor_sit"
Parameters
text:CharSequence

text to search and replace in, may be null.

regex:Pattern

the regular expression pattern to which this string is to be matched.

replacement:String

the string to be substituted for each match.

Returns:String

the text with any replacements processed, null if null String input.

See Also
java.util.regex.Matcher#replaceAll(String), java.util.regex.Pattern
replaceAllback to summary
public static String replaceAll(final String text, final Pattern regex, final String replacement)

Replaces each 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).replaceAll(replacement)

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

StringUtils.replaceAll(null, *, *)       = null
StringUtils.replaceAll("any", (Pattern) null, *)   = "any"
StringUtils.replaceAll("any", *, null)   = "any"
StringUtils.replaceAll("", Pattern.compile(""), "zzz")    = "zzz"
StringUtils.replaceAll("", Pattern.compile(".*"), "zzz")  = "zzz"
StringUtils.replaceAll("", Pattern.compile(".+"), "zzz")  = ""
StringUtils.replaceAll("abc", Pattern.compile(""), "ZZ")  = "ZZaZZbZZcZZ"
StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>"), "z")                 = "z\nz"
StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>", Pattern.DOTALL), "z") = "z"
StringUtils.replaceAll("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z")             = "z"
StringUtils.replaceAll("ABCabc123", Pattern.compile("[a-z]"), "_")       = "ABC___123"
StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "_")  = "ABC_123"
StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "")   = "ABC123"
StringUtils.replaceAll("Lorem ipsum  dolor   sit", Pattern.compile("( +)([a-z]+)"), "_$2")  = "Lorem_ipsum_dolor_sit"
Parameters
text:String

text to search and replace in, may be null.

regex:Pattern

the regular expression pattern to which this string is to be matched.

replacement:String

the string to be substituted for each match.

Returns:String

the text with any replacements processed, null if null String input.

Annotations
@Deprecated
See Also
java.util.regex.Matcher#replaceAll(String), java.util.regex.Pattern
replaceAllback to summary
public static String replaceAll(final String text, final String regex, final String replacement)

Replaces each substring of the text String that matches the given regular expression with the given replacement. This method is a null safe equivalent to:

  • text.replaceAll(regex, replacement)
  • Pattern.compile(regex).matcher(text).replaceAll(replacement)

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

Unlike in the replacePattern(CharSequence, String, String) method, the Pattern#DOTALL option is NOT automatically added. To use the DOTALL option prepend "(?s)" to the regex. DOTALL is also known as single-line mode in Perl.

StringUtils.replaceAll(null, *, *)       = null
StringUtils.replaceAll("any", (String) null, *)   = "any"
StringUtils.replaceAll("any", *, null)   = "any"
StringUtils.replaceAll("", "", "zzz")    = "zzz"
StringUtils.replaceAll("", ".*", "zzz")  = "zzz"
StringUtils.replaceAll("", ".+", "zzz")  = ""
StringUtils.replaceAll("abc", "", "ZZ")  = "ZZaZZbZZcZZ"
StringUtils.replaceAll("<__>\n<__>", "<.*>", "z")      = "z\nz"
StringUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z")  = "z"
StringUtils.replaceAll("ABCabc123", "[a-z]", "_")       = "ABC___123"
StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "_")  = "ABC_123"
StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "")   = "ABC123"
StringUtils.replaceAll("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2")  = "Lorem_ipsum_dolor_sit"
Parameters
text:String

text to search and replace in, may be null.

regex:String

the regular expression to which this string is to be matched.

replacement:String

the string to be substituted for each match.

Returns:String

the text with any replacements processed, null if null String input.

Exceptions
PatternSyntaxException:
if the regular expression's syntax is invalid.
See Also
replacePattern(String, String, String), String#replaceAll(String, String), java.util.regex.Pattern, java.util.regex.Pattern#DOTALL
replaceFirstback to summary
public static String replaceFirst(final CharSequence 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"
Parameters
text:CharSequence

text to search and replace in, may be null.

regex:Pattern

the regular expression pattern to which this string is to be matched.

replacement:String

the string to be substituted for the first match

Returns:String

the text with the first replacement processed, null if null String input.

Since
3.18.0
See Also
java.util.regex.Matcher#replaceFirst(String), java.util.regex.Pattern
replaceFirstback to summary
public static String replaceFirst(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"
Parameters
text:String

text to search and replace in, may be null.

regex:Pattern

the regular expression pattern to which this string is to be matched.

replacement:String

the string to be substituted for the first match.

Returns:String

the text with the first replacement processed, null if null String input.

Annotations
@Deprecated
See Also
java.util.regex.Matcher#replaceFirst(String), java.util.regex.Pattern
replaceFirstback to summary
public static String replaceFirst(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. To use the DOTALL option prepend "(?s)" to the regex. DOTALL is also known as single-line mode in Perl.

StringUtils.replaceFirst(null, *, *)       = null
StringUtils.replaceFirst("any", (String) null, *)   = "any"
StringUtils.replaceFirst("any", *, null)   = "any"
StringUtils.replaceFirst("", "", "zzz")    = "zzz"
StringUtils.replaceFirst("", ".*", "zzz")  = "zzz"
StringUtils.replaceFirst("", ".+", "zzz")  = ""
StringUtils.replaceFirst("abc", "", "ZZ")  = "ZZabc"
StringUtils.replaceFirst("<__>\n<__>", "<.*>", "z")      = "z\n<__>"
StringUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z")  = "z"
StringUtils.replaceFirst("ABCabc123", "[a-z]", "_")          = "ABC_bc123"
StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_")  = "ABC_123abc"
StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "")   = "ABC123abc"
StringUtils.replaceFirst("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2")  = "Lorem_ipsum  dolor   sit"
Parameters
text:String

text to search and replace in, may be null.

regex:String

the regular expression to which this string is to be matched.

replacement:String

the string to be substituted for the first match.

Returns:String

the text with the first replacement processed, null if null String input.

Exceptions
PatternSyntaxException:
if the regular expression's syntax is invalid.
See Also
String#replaceFirst(String, String), java.util.regex.Pattern, java.util.regex.Pattern#DOTALL
replacePatternback to summary
public static String replacePattern(final CharSequence text, final String regex, final String replacement)

Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern#DOTALL option. DOTALL is also known as single-line mode in Perl. This call is a null safe equivalent to:

  • text.replaceAll("(?s)" + regex, replacement)
  • Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(replacement)

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

StringUtils.replacePattern(null, *, *)       = null
StringUtils.replacePattern("any", (String) null, *)   = "any"
StringUtils.replacePattern("any", *, null)   = "any"
StringUtils.replacePattern("", "", "zzz")    = "zzz"
StringUtils.replacePattern("", ".*", "zzz")  = "zzz"
StringUtils.replacePattern("", ".+", "zzz")  = ""
StringUtils.replacePattern("<__>\n<__>", "<.*>", "z")       = "z"
StringUtils.replacePattern("ABCabc123", "[a-z]", "_")       = "ABC___123"
StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_")  = "ABC_123"
StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "")   = "ABC123"
StringUtils.replacePattern("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2")  = "Lorem_ipsum_dolor_sit"
Parameters
text:CharSequence

the source string.

regex:String

the regular expression to which this string is to be matched.

replacement:String

the string to be substituted for each match.

Returns:String

The resulting String.

Since
3.18.0
See Also
replaceAll(String, String, String), String#replaceAll(String, String), Pattern#DOTALL
replacePatternback to summary
public static String replacePattern(final String text, final String regex, final String replacement)

Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern#DOTALL option. DOTALL is also known as single-line mode in Perl. This call is a null safe equivalent to:

  • text.replaceAll("(?s)" + regex, replacement)
  • Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(replacement)

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

StringUtils.replacePattern(null, *, *)       = null
StringUtils.replacePattern("any", (String) null, *)   = "any"
StringUtils.replacePattern("any", *, null)   = "any"
StringUtils.replacePattern("", "", "zzz")    = "zzz"
StringUtils.replacePattern("", ".*", "zzz")  = "zzz"
StringUtils.replacePattern("", ".+", "zzz")  = ""
StringUtils.replacePattern("<__>\n<__>", "<.*>", "z")       = "z"
StringUtils.replacePattern("ABCabc123", "[a-z]", "_")       = "ABC___123"
StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_")  = "ABC_123"
StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "")   = "ABC123"
StringUtils.replacePattern("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2")  = "Lorem_ipsum_dolor_sit"
Parameters
text:String

the source string.

regex:String

the regular expression to which this string is to be matched.

replacement:String

the string to be substituted for each match.

Returns:String

The resulting String.

Annotations
@Deprecated
See Also
replaceAll(String, String, String), String#replaceAll(String, String), Pattern#DOTALL
toStringOrNullback to summary
private static String toStringOrNull(final CharSequence text)