Class RegexUtil


  • public class RegexUtil
    extends Object
    Collection of helper function for working with regular expressions.
    • Constructor Detail

      • RegexUtil

        public RegexUtil()
    • Method Detail

      • getPattern

        public static Pattern getPattern​(String regex)
      • find

        public static String find​(String searchIn,
                                  String findWhatRegex,
                                  int group)
      • countMatches

        public static int countMatches​(String string,
                                       String regex)
      • countMatches

        public static int countMatches​(String string,
                                       String regex,
                                       int matchLen)
      • countLeadingQualifiers

        public static int countLeadingQualifiers​(String string,
                                                 String qualifier)
      • countTrailingQualifiers

        public static int countTrailingQualifiers​(String string,
                                                  String qualifier)
      • escape

        public static String escape​(String str)
        Escapes a given string for regex.
        Parameters:
        str - the given string
        Returns:
        escaped string
      • matches

        public static boolean matches​(String st,
                                      Pattern pattern)
      • contains

        public static boolean contains​(String st,
                                       Pattern pattern)
      • getQuotedAreas

        public static List<Range> getQuotedAreas​(String regex)
      • getGroupAtPos

        public static int getGroupAtPos​(String regex,
                                        int position)
      • updateGroupReferences

        public static String updateGroupReferences​(String regex,
                                                   int groupNum)
        Adjust values in back references to capturing groups (like \1) of a given regex. This method needs to be called when a new group is added to a regex, and the regex contains back references to existing groups. Values in the references having number equal or greater than groupNum, should be increased by 1.
        Parameters:
        regex - the given regex containing back references to capturing groups.
        groupNum - the number of the new group.
        Returns:
        the given regex with updated references.