@Immutable public final class RegExHelper extends Object
RegExPool.| Modifier and Type | Method and Description |
|---|---|
static String[] |
getAllMatchingGroupValues(String sRegEx,
String sValue)
Get the values of all groups (RegEx
(...) |
static String |
getAsIdentifier(String s)
Convert an identifier to a programming language identifier by replacing all
non-word characters with an underscore ("_").
|
static String |
getAsIdentifier(String s,
char cReplacement)
Convert an identifier to a programming language identifier by replacing all
non-word characters with an underscore.
|
static String |
getAsIdentifier(String s,
String sReplacement)
Convert an identifier to a programming language identifier by replacing all
non-word characters with an underscore.
|
static Matcher |
getMatcher(String sRegEx,
int nOptions,
String sValue)
Get the Java Matcher object for the passed pair of regular expression and
value.
|
static Matcher |
getMatcher(String sRegEx,
String sValue)
Get the Java Matcher object for the passed pair of regular expression and
value.
|
static String[] |
getSplitToArray(CharSequence sText,
String sRegEx)
Split the passed text with the given regular expression.
|
static String[] |
getSplitToArray(CharSequence sText,
String sRegEx,
int nLimit)
Split the passed text with the given regular expression returning at most
the given number of tokens.
|
static List<String> |
getSplitToList(CharSequence sText,
String sRegEx)
Split the passed text with the given regular expression.
|
static List<String> |
getSplitToList(CharSequence sText,
String sRegEx,
int nLimit)
Split the passed text with the given regular expression.
|
static boolean |
isValidPattern(String sRegEx)
Check if the passed regular expression is invalid.
|
static boolean |
stringMatchesPattern(String sRegEx,
int nOptions,
String sValue)
A shortcut helper method to determine whether a string matches a certain
regular expression or not.
|
static boolean |
stringMatchesPattern(String sRegEx,
String sValue)
A shortcut helper method to determine whether a string matches a certain
regular expression or not.
|
static String |
stringReplacePattern(String sRegEx,
int nOptions,
String sValue,
String sReplacement) |
static String |
stringReplacePattern(String sRegEx,
String sValue,
String sReplacement) |
@Nonnull public static String[] getSplitToArray(@Nullable CharSequence sText, @Nonnull @RegEx String sRegEx)
StringHelper.getExploded(String, String) is a
faster option.sText - The text to be split. May be null.sRegEx - The regular expression to use for splitting. May neither be
null nor empty.null, a non-
null array otherwise. If both text and regular
expression are null an empty array is returned as well
since the text parameter is checked first.@Nonnull public static String[] getSplitToArray(@Nullable CharSequence sText, @Nonnull @RegEx String sRegEx, @Nonnegative int nLimit)
StringHelper.getExploded(String, String, int) is a faster option.sText - The text to be split. May be null.sRegEx - The regular expression to use for splitting. May neither be
null nor empty.nLimit - The maximum number of tokens to return if the value is > 0. If
the value is ≤ 0 it has no effect and all tokens are returned.null, a non-
null array otherwise. If both text and regular
expression are null an empty array is returned as well
since the text parameter is checked first.@Nonnull public static List<String> getSplitToList(@Nullable CharSequence sText, @Nonnull @RegEx String sRegEx)
StringHelper.getExploded(String, String) is a
faster option.sText - The text to be split. May be null.sRegEx - The regular expression to use for splitting. May neither be
null nor empty.null, a non-
null list otherwise. If both text and regular
expression are null an empty list is returned as well
since the text parameter is checked first.@Nonnull public static List<String> getSplitToList(@Nullable CharSequence sText, @Nonnull @RegEx String sRegEx, @Nonnegative int nLimit)
StringHelper.getExploded(String, String, int)
is a faster option.sText - The text to be split. May be null.sRegEx - The regular expression to use for splitting. May neither be
null nor empty.nLimit - The maximum number of tokens to return if the value is > 0. If
the value is ≤ 0 it has no effect and all tokens are returned.null, a non-
null list otherwise. If both text and regular
expression are null an empty list is returned as well
since the text parameter is checked first.@Nonnull public static Matcher getMatcher(@Nonnull @RegEx String sRegEx, @Nonnull String sValue)
sRegEx - The regular expression to use. May neither be null nor
empty.sValue - The value to create the matcher for. May not be null.null matcher.@Nonnull public static Matcher getMatcher(@Nonnull @RegEx String sRegEx, @Nonnegative int nOptions, @Nonnull String sValue)
sRegEx - The regular expression to use. May neither be null nor
empty.nOptions - The pattern compilations options to be used.sValue - The value to create the matcher for. May not be null.null matcher.Pattern.compile(String, int)public static boolean stringMatchesPattern(@Nonnull @RegEx String sRegEx, @Nonnull String sValue)
sRegEx - The regular expression to be used. The compiled regular expression
pattern is cached. May neither be null nor empty.sValue - The string value to compare against the regular expression.true if the string matches the regular expression,
false otherwise.public static boolean stringMatchesPattern(@Nonnull @RegEx String sRegEx, @Nonnegative int nOptions, @Nonnull String sValue)
sRegEx - The regular expression to be used. The compiled regular expression
pattern is cached. May neither be null nor empty.nOptions - The pattern compilations options to be used.sValue - The string value to compare against the regular expression.true if the string matches the regular expression,
false otherwise.Pattern.compile(String, int)@Nonnull public static String stringReplacePattern(@Nonnull @RegEx String sRegEx, @Nonnull String sValue, @Nullable String sReplacement)
@Nonnull public static String stringReplacePattern(@Nonnull @RegEx String sRegEx, @Nonnegative int nOptions, @Nonnull String sValue, @Nullable String sReplacement)
@Nullable public static String getAsIdentifier(@Nullable String s)
s - The string to convert. May be null or empty.null if the input string is
null.@Nullable public static String getAsIdentifier(@Nullable String s, char cReplacement)
s - The string to convert. May be null or empty.cReplacement - The replacement character to be used for all non-identifier
charactersnull if the input string is
null.@Nullable public static String getAsIdentifier(@Nullable String s, @Nonnull String sReplacement)
s - The string to convert. May be null or empty.sReplacement - The replacement string to be used for all non-identifier characters.
May be empty but not be null.null if the input string is
null. Maybe an invalid identifier, if the replacement
is empty, and the identifier starts with an illegal character, or
if the replacement is empty and the source string only contains
invalid characters!public static boolean isValidPattern(@Nonnull @RegEx String sRegEx)
Pattern.compile(String) each time, which is CPU intensive and has a
synchronization penalty.sRegEx - The regular expression to validate. May not be null.true if the pattern is valid, false
otherwise.@Nullable public static String[] getAllMatchingGroupValues(@Nonnull @RegEx String sRegEx, @Nonnull String sValue)
(...)) for the passed
value.(?:a|b))sRegEx - The regular expression containing the groupssValue - The value to checknull if the passed value does not match the regular
expression. An empty array if the regular expression contains no
capturing group.Copyright © 2014–2016 Philip Helger. All rights reserved.