On this page

RegExUtils

public class org.apache.commons.lang3.RegExUtils[source]

Helpers to process Strings using regular expressions.

See also:

java.util.regex.Pattern

Since:

3.8

Constructors

RegExUtils()

Deprecated. Make private in 4.0.

@Deprecated public RegExUtils()[source]

Deprecated: TODO Make private in 4.0.

Make private in 4.0.

Methods

dotAll(String)

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

dotAllMatcher(String, CharSequence)

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.

dotAllMatcher(String, String)

Deprecated. 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.

removeAll(CharSequence, Pattern)

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

removeAll(String, String)

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

removeAll(String, Pattern)

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

removeFirst(CharSequence, Pattern)

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

removeFirst(String, String)

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

removeFirst(String, Pattern)

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

removePattern(CharSequence, String)

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

removePattern(String, String)

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

replaceAll(CharSequence, Pattern, String)

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

replaceAll(String, String, String)

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

replaceAll(String, Pattern, String)

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

replaceFirst(CharSequence, Pattern, String)

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

replaceFirst(String, String, String)

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

replaceFirst(String, Pattern, String)

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

replacePattern(CharSequence, String, String)

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

replacePattern(String, String, String)

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

public static Pattern dotAll(String regex)[source]

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

Parameters:

regex – The expression to be compiled.

Returns:

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

Since:

3.13.0

public static Matcher dotAllMatcher(String regex, CharSequence text)[source]

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 – The expression to be compiled.

  • text – The character sequence to be matched.

Returns:

A new matcher for this pattern.

Since:

3.18.0

@Deprecated public static Matcher dotAllMatcher(String regex, String text)[source]

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 – The expression to be compiled.

  • text – The character sequence to be matched.

Returns:

A new matcher for this pattern.

Since:

3.13.0

public static String removeAll(CharSequence text, Pattern regex)[source]

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.

RegExUtils.removeAll(null, *)      = null
RegExUtils.removeAll("any", (Pattern) null)  = "any"
RegExUtils.removeAll("any", Pattern.compile(""))    = "any"
RegExUtils.removeAll("any", Pattern.compile(".*"))  = ""
RegExUtils.removeAll("any", Pattern.compile(".+"))  = ""
RegExUtils.removeAll("abc", Pattern.compile(".?"))  = ""
RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>"))      = "A\nB"
RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))  = "AB"
RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>", Pattern.DOTALL))  = "AB"
RegExUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]"))     = "ABC123"
Parameters:
  • text – text to remove from, may be null.

  • regex – The regular expression to which this string is to be matched.

Returns:

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

See also:
Since:

3.18.0

public static String removeAll(String text, String regex)[source]

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.

RegExUtils.removeAll(null, *)      = null
RegExUtils.removeAll("any", (String) null)  = "any"
RegExUtils.removeAll("any", "")    = "any"
RegExUtils.removeAll("any", ".*")  = ""
RegExUtils.removeAll("any", ".+")  = ""
RegExUtils.removeAll("abc", ".?")  = ""
RegExUtils.removeAll("A<__>\n<__>B", "<.*>")      = "A\nB"
RegExUtils.removeAll("A<__>\n<__>B", "(?s)<.*>")  = "AB"
RegExUtils.removeAll("ABCabc123abc", "[a-z]")     = "ABC123"
Parameters:
  • text – text to remove from, may be null

  • regex – The regular expression to which this string is to be matched

Returns:

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

Throws:

java.util.regex.PatternSyntaxException – Thrown if the regular expression’s syntax is invalid.

See also:
@Deprecated public static String removeAll(String text, Pattern regex)[source]

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.

RegExUtils.removeAll(null, *)      = null
RegExUtils.removeAll("any", (Pattern) null)  = "any"
RegExUtils.removeAll("any", Pattern.compile(""))    = "any"
RegExUtils.removeAll("any", Pattern.compile(".*"))  = ""
RegExUtils.removeAll("any", Pattern.compile(".+"))  = ""
RegExUtils.removeAll("abc", Pattern.compile(".?"))  = ""
RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>"))      = "A\nB"
RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))  = "AB"
RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>", Pattern.DOTALL))  = "AB"
RegExUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]"))     = "ABC123"
Parameters:
  • text – text to remove from, may be null.

  • regex – The regular expression to which this string is to be matched

Returns:

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

See also:
public static String removeFirst(CharSequence text, Pattern regex)[source]

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.

RegExUtils.removeFirst(null, *)      = null
RegExUtils.removeFirst("any", (Pattern) null)  = "any"
RegExUtils.removeFirst("any", Pattern.compile(""))    = "any"
RegExUtils.removeFirst("any", Pattern.compile(".*"))  = ""
RegExUtils.removeFirst("any", Pattern.compile(".+"))  = ""
RegExUtils.removeFirst("abc", Pattern.compile(".?"))  = "bc"
RegExUtils.removeFirst("A<__>\n<__>B", Pattern.compile("<.*>"))      = "A\n<__>B"
RegExUtils.removeFirst("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))  = "AB"
RegExUtils.removeFirst("ABCabc123", Pattern.compile("[a-z]"))          = "ABCbc123"
RegExUtils.removeFirst("ABCabc123abc", Pattern.compile("[a-z]+"))      = "ABC123abc"
Parameters:
  • text – text to remove from, may be null.

  • regex – The regular expression pattern to which this string is to be matched.

Returns:

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

See also:
Since:

3.18.0

public static String removeFirst(String text, String regex)[source]

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.

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

  • regex – The regular expression to which this string is to be matched.

Returns:

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

Throws:

java.util.regex.PatternSyntaxException – Thrown if the regular expression’s syntax is invalid.

See also:
@Deprecated public static String removeFirst(String text, Pattern regex)[source]

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.

RegExUtils.removeFirst(null, *)      = null
RegExUtils.removeFirst("any", (Pattern) null)  = "any"
RegExUtils.removeFirst("any", Pattern.compile(""))    = "any"
RegExUtils.removeFirst("any", Pattern.compile(".*"))  = ""
RegExUtils.removeFirst("any", Pattern.compile(".+"))  = ""
RegExUtils.removeFirst("abc", Pattern.compile(".?"))  = "bc"
RegExUtils.removeFirst("A<__>\n<__>B", Pattern.compile("<.*>"))      = "A\n<__>B"
RegExUtils.removeFirst("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))  = "AB"
RegExUtils.removeFirst("ABCabc123", Pattern.compile("[a-z]"))          = "ABCbc123"
RegExUtils.removeFirst("ABCabc123abc", Pattern.compile("[a-z]+"))      = "ABC123abc"
Parameters:
  • text – text to remove from, may be null.

  • regex – The regular expression pattern to which this string is to be matched.

Returns:

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

See also:
public static String removePattern(CharSequence text, String regex)[source]

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(&quot;(?s)&quot; + regex, StringUtils.EMPTY)

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

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

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

  • regex – the regular expression to which this string is to be matched.

Returns:

The resulting String.

See also:
Since:

3.18.0

@Deprecated public static String removePattern(String text, String regex)[source]

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(&quot;(?s)&quot; + regex, StringUtils.EMPTY)

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

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

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

  • regex – the regular expression to which this string is to be matched.

Returns:

The resulting String.

See also:
public static String replaceAll(CharSequence text, Pattern regex, String replacement)[source]

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.

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

  • regex – The regular expression pattern to which this string is to be matched.

  • replacement – The string to be substituted for each match.

Returns:

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

See also:
public static String replaceAll(String text, String regex, String replacement)[source]

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.

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

  • regex – The regular expression to which this string is to be matched.

  • replacement – The string to be substituted for each match.

Returns:

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

Throws:

java.util.regex.PatternSyntaxException – Thrown if the regular expression’s syntax is invalid.

See also:
@Deprecated public static String replaceAll(String text, Pattern regex, String replacement)[source]

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.

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

  • regex – The regular expression pattern to which this string is to be matched.

  • replacement – The string to be substituted for each match.

Returns:

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

See also:
public static String replaceFirst(CharSequence text, Pattern regex, String replacement)[source]

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.

RegExUtils.replaceFirst(null, *, *)       = null
RegExUtils.replaceFirst("any", (Pattern) null, *)   = "any"
RegExUtils.replaceFirst("any", *, null)   = "any"
RegExUtils.replaceFirst("", Pattern.compile(""), "zzz")    = "zzz"
RegExUtils.replaceFirst("", Pattern.compile(".*"), "zzz")  = "zzz"
RegExUtils.replaceFirst("", Pattern.compile(".+"), "zzz")  = ""
RegExUtils.replaceFirst("abc", Pattern.compile(""), "ZZ")  = "ZZabc"
RegExUtils.replaceFirst("<__>\n<__>", Pattern.compile("<.*>"), "z")      = "z\n<__>"
RegExUtils.replaceFirst("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z")  = "z"
RegExUtils.replaceFirst("ABCabc123", Pattern.compile("[a-z]"), "_")          = "ABC_bc123"
RegExUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "_")  = "ABC_123abc"
RegExUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "")   = "ABC123abc"
RegExUtils.replaceFirst("Lorem ipsum  dolor   sit", Pattern.compile("( +)([a-z]+)"), "_$2")  = "Lorem_ipsum  dolor   sit"
Parameters:
  • text – text to search and replace in, may be null.

  • regex – The regular expression pattern to which this string is to be matched.

  • replacement – The string to be substituted for the first match

Returns:

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

See also:
Since:

3.18.0

public static String replaceFirst(String text, String regex, String replacement)[source]

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.

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

  • regex – The regular expression to which this string is to be matched.

  • replacement – The string to be substituted for the first match.

Returns:

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

Throws:

java.util.regex.PatternSyntaxException – Thrown if the regular expression’s syntax is invalid.

See also:
@Deprecated public static String replaceFirst(String text, Pattern regex, String replacement)[source]

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.

RegExUtils.replaceFirst(null, *, *)       = null
RegExUtils.replaceFirst("any", (Pattern) null, *)   = "any"
RegExUtils.replaceFirst("any", *, null)   = "any"
RegExUtils.replaceFirst("", Pattern.compile(""), "zzz")    = "zzz"
RegExUtils.replaceFirst("", Pattern.compile(".*"), "zzz")  = "zzz"
RegExUtils.replaceFirst("", Pattern.compile(".+"), "zzz")  = ""
RegExUtils.replaceFirst("abc", Pattern.compile(""), "ZZ")  = "ZZabc"
RegExUtils.replaceFirst("<__>\n<__>", Pattern.compile("<.*>"), "z")      = "z\n<__>"
RegExUtils.replaceFirst("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z")  = "z"
RegExUtils.replaceFirst("ABCabc123", Pattern.compile("[a-z]"), "_")          = "ABC_bc123"
RegExUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "_")  = "ABC_123abc"
RegExUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "")   = "ABC123abc"
RegExUtils.replaceFirst("Lorem ipsum  dolor   sit", Pattern.compile("( +)([a-z]+)"), "_$2")  = "Lorem_ipsum  dolor   sit"
Parameters:
  • text – text to search and replace in, may be null.

  • regex – The regular expression pattern to which this string is to be matched.

  • replacement – The string to be substituted for the first match.

Returns:

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

See also:
public static String replacePattern(CharSequence text, String regex, String replacement)[source]

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(&quot;(?s)&quot; + regex, replacement)

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

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

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

  • regex – the regular expression to which this string is to be matched.

  • replacement – the string to be substituted for each match.

Returns:

The resulting String.

See also:
Since:

3.18.0

@Deprecated public static String replacePattern(String text, String regex, String replacement)[source]

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(&quot;(?s)&quot; + regex, replacement)

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

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

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

  • regex – the regular expression to which this string is to be matched.

  • replacement – the string to be substituted for each match.

Returns:

The resulting String.

See also: