Class RegexUtils

java.lang.Object
com.renomad.minum.testing.RegexUtils

public final class RegexUtils extends Object
Handy helpers to make regular expression marginally easier / more efficient, etc.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    find(String regex, String data)
    Helper to find a value in a string using a Regex.
    static String
    find(String regex, String data, String matchGroupName)
    Find a value by regular expression, for testing
    static boolean
    isFound(String regex, String data)
    Returns whether the regular expression matched the data Note: This is a poor-performance method, mainly used as a quick helper where performance concerns don't exist,since each call to this method will compile the regular expression.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • find

      public static String find(String regex, String data)
      Helper to find a value in a string using a Regex. Note, this is not nearly as performant, since each call to this method will compile the regular expression.
      Returns:
      returns the first match found, or an empty string
    • isFound

      public static boolean isFound(String regex, String data)
      Returns whether the regular expression matched the data Note: This is a poor-performance method, mainly used as a quick helper where performance concerns don't exist,since each call to this method will compile the regular expression.
    • find

      public static String find(String regex, String data, String matchGroupName)
      Find a value by regular expression, for testing

      A helper method to make things it easier to find a value in a string using a Regex. This method is slow, since each call will compile the regular expression.

      This version is similar to find(String, String) except that it allows you to specify a match group by name. For example, here's a regex with a named match group, in this example the name is "namevalue":

               "\\bname\\b=\"(?<namevalue>.*?)\""
           

      Thus, to use it here, you would search like this:

               find("\\bname\\b=\"(?<namevalue>.*?)\"", data, "namevalue")
           

      To summarize: in a regex, you specify a matching group by surrounding it with parentheses. To name it, you insert right after the opening parenthesis a question mark and then a string literal surrounded by angle brackets

      Important: the name of the match group must be alphanumeric - do not use any special characters or punctuation

      Returns:
      returns the first match found, or an empty string