Class StringRegexExtension


  • public class StringRegexExtension
    extends Object
    Extension regex methods for the String class.
    • Constructor Detail

      • StringRegexExtension

        public StringRegexExtension()
    • Method Detail

      • replaceFirst

        public static String replaceFirst​(String self,
                                          String regex,
                                          String replacement)
        Replaces the first substring of this string that matches the given regular expression with the given replacement.
        Parameters:
        self - the original string, corresponding to this, to replace the first match from
        regex - the regular expression to which this string is to be matched
        replacement - the string to be substituted for the first match
        Returns:
        The resulting String
        Throws:
        PatternSyntaxException - if the regular expression's syntax is invalid
      • replaceAll

        public static String replaceAll​(String self,
                                        String regex,
                                        String replacement)
        Replaces each substring of this string that matches the given regular expression with the given replacement.
        Parameters:
        self - the original string, corresponding to this, to replace matches from
        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
        Throws:
        PatternSyntaxException - if the regular expression's syntax is invalid
      • split

        public static String[] split​(String self,
                                     String regex)
        Splits this string around matches of the given regular expression. Trailing empty strings are not included in the resulting array.
        Parameters:
        self - the original string, corresponding to this, to be split
        regex - the delimiting regular expression
        Returns:
        the array of strings computed by splitting this string around matches of the given regular expression
        Throws:
        PatternSyntaxException - if the regular expression's syntax is invalid
      • matches

        public static boolean matches​(String self,
                                      String regex)
        Tells whether or not this string matches the given regular expression.
        Parameters:
        self - the original string, corresponding to this, to check for matching
        regex - the regular expression to which this string is to be matched
        Returns:
        true if, and only if, this string matches the given regular expression
        Throws:
        PatternSyntaxException - if the regular expression's syntax is invalid