Package com.helger.commons.regex
Class RegExHelper
java.lang.Object
com.helger.commons.regex.RegExHelper
This class offers helper methods that work on cached regular expression
pattern as offered by
RegExCache.- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic String[]getAllMatchingGroupValues(String sRegEx, String sValue) Get the values of all groups (RegEx(...)) for the passed value.
Note: groups starting with "?static StringConvert an identifier to a programming language identifier by replacing all non-word characters with an underscore ("_").static StringgetAsIdentifier(String s, char cReplacement) Convert an identifier to a programming language identifier by replacing all non-word characters with an underscore.static StringgetAsIdentifier(String s, String sReplacement) Convert an identifier to a programming language identifier by replacing all non-word characters with an underscore.static MatchergetMatcher(String sRegEx, int nOptions, String sValue) Get the Java Matcher object for the passed pair of regular expression and value.static MatchergetMatcher(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 ICommonsList<String> getSplitToList(CharSequence sText, String sRegEx) Split the passed text with the given regular expression.static ICommonsList<String> getSplitToList(CharSequence sText, String sRegEx, int nLimit) Split the passed text with the given regular expression.static booleanisValidPattern(String sRegEx) Check if the passed regular expression is invalid.
Note: this method may be a performance killer, as it callsPattern.compile(String)each time, which is CPU intensive and has a synchronization penalty.static booleanstringMatchesPattern(String sRegEx, int nOptions, String sValue) A shortcut helper method to determine whether a string matches a certain regular expression or not.static booleanstringMatchesPattern(String sRegEx, String sValue) A shortcut helper method to determine whether a string matches a certain regular expression or not.static StringstringReplacePattern(String sRegEx, int nOptions, String sValue, String sReplacement) static StringstringReplacePattern(String sRegEx, String sValue, String sReplacement)
-
Method Details
-
getSplitToArray
@Nonnull public static String[] getSplitToArray(@Nullable CharSequence sText, @Nonnull @RegEx String sRegEx) Split the passed text with the given regular expression. If you do not need a regular expression,StringHelper.getExploded(String, String)is a faster option.- Parameters:
sText- The text to be split. May benull.sRegEx- The regular expression to use for splitting. May neither benullnor empty.- Returns:
- An empty array if the text is
null, a non-nullarray otherwise. If both text and regular expression arenullan empty array is returned as well since the text parameter is checked first.
-
getSplitToArray
@Nonnull public static String[] getSplitToArray(@Nullable CharSequence sText, @Nonnull @RegEx String sRegEx, @Nonnegative int nLimit) Split the passed text with the given regular expression returning at most the given number of tokens. If you do not need a regular expression,StringHelper.getExploded(String, String, int)is a faster option.- Parameters:
sText- The text to be split. May benull.sRegEx- The regular expression to use for splitting. May neither benullnor 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.- Returns:
- An empty array if the text is
null, a non-nullarray otherwise. If both text and regular expression arenullan empty array is returned as well since the text parameter is checked first.
-
getSplitToList
@Nonnull public static ICommonsList<String> getSplitToList(@Nullable CharSequence sText, @Nonnull @RegEx String sRegEx) Split the passed text with the given regular expression. If you do not need a regular expression,StringHelper.getExploded(String, String)is a faster option.- Parameters:
sText- The text to be split. May benull.sRegEx- The regular expression to use for splitting. May neither benullnor empty.- Returns:
- An empty list if the text is
null, a non-nulllist otherwise. If both text and regular expression arenullan empty list is returned as well since the text parameter is checked first.
-
getSplitToList
@Nonnull public static ICommonsList<String> getSplitToList(@Nullable CharSequence sText, @Nonnull @RegEx String sRegEx, @Nonnegative int nLimit) Split the passed text with the given regular expression. If you do not need a regular expression,StringHelper.getExploded(String, String, int)is a faster option.- Parameters:
sText- The text to be split. May benull.sRegEx- The regular expression to use for splitting. May neither benullnor 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.- Returns:
- An empty list if the text is
null, a non-nulllist otherwise. If both text and regular expression arenullan empty list is returned as well since the text parameter is checked first.
-
getMatcher
Get the Java Matcher object for the passed pair of regular expression and value.- Parameters:
sRegEx- The regular expression to use. May neither benullnor empty.sValue- The value to create the matcher for. May not benull.- Returns:
- A non-
nullmatcher.
-
getMatcher
@Nonnull public static Matcher getMatcher(@Nonnull @RegEx String sRegEx, @Nonnegative int nOptions, @Nonnull String sValue) Get the Java Matcher object for the passed pair of regular expression and value.- Parameters:
sRegEx- The regular expression to use. May neither benullnor empty.nOptions- The pattern compilations options to be used.sValue- The value to create the matcher for. May not benull.- Returns:
- A non-
nullmatcher. - See Also:
-
stringMatchesPattern
A shortcut helper method to determine whether a string matches a certain regular expression or not.- Parameters:
sRegEx- The regular expression to be used. The compiled regular expression pattern is cached. May neither benullnor empty.sValue- The string value to compare against the regular expression.- Returns:
trueif the string matches the regular expression,falseotherwise.
-
stringMatchesPattern
public static boolean stringMatchesPattern(@Nonnull @RegEx String sRegEx, @Nonnegative int nOptions, @Nonnull String sValue) A shortcut helper method to determine whether a string matches a certain regular expression or not.- Parameters:
sRegEx- The regular expression to be used. The compiled regular expression pattern is cached. May neither benullnor empty.nOptions- The pattern compilations options to be used.sValue- The string value to compare against the regular expression.- Returns:
trueif the string matches the regular expression,falseotherwise.- See Also:
-
stringReplacePattern
-
stringReplacePattern
-
getAsIdentifier
Convert an identifier to a programming language identifier by replacing all non-word characters with an underscore ("_").- Parameters:
s- The string to convert. May benullor empty.- Returns:
- The converted string or
nullif the input string isnull.
-
getAsIdentifier
Convert an identifier to a programming language identifier by replacing all non-word characters with an underscore.- Parameters:
s- The string to convert. May benullor empty.cReplacement- The replacement character to be used for all non-identifier characters- Returns:
- The converted string or
nullif the input string isnull.
-
getAsIdentifier
Convert an identifier to a programming language identifier by replacing all non-word characters with an underscore.- Parameters:
s- The string to convert. May benullor empty.sReplacement- The replacement string to be used for all non-identifier characters. May be empty but not benull.- Returns:
- The converted string or
nullif the input string isnull. 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!
-
isValidPattern
Check if the passed regular expression is invalid.
Note: this method may be a performance killer, as it callsPattern.compile(String)each time, which is CPU intensive and has a synchronization penalty.- Parameters:
sRegEx- The regular expression to validate. May not benull.- Returns:
trueif the pattern is valid,falseotherwise.
-
getAllMatchingGroupValues
@Nullable public static String[] getAllMatchingGroupValues(@Nonnull @RegEx String sRegEx, @Nonnull String sValue) Get the values of all groups (RegEx(...)) for the passed value.
Note: groups starting with "?:" are non-capturing groups (e.g.(?:a|b))- Parameters:
sRegEx- The regular expression containing the groupssValue- The value to check- Returns:
nullif the passed value does not match the regular expression. An empty array if the regular expression contains no capturing group.
-