Class RgxApi

java.lang.Object
dev.comfast.rgx.RgxApi

public class RgxApi extends Object
Provide fluent interface for common Regex operations. Most cases can be done in one line with proper error handling: Usage example:

 rgx("\\d+").match("abc123xxx456").get() // returns "123"
 rgx("\\d+").match("abc123xxx456").matchAllAsString() // returns entire matches: ["123", "456"]
 rgx("\\d(\\d\\d)").match("abc123xxx456").matchAllAsString(1) // returns groups number '1': ["23", "56"]
 rgx("\\d(\\d\\d)").match("abc123xxx456").group(1) // returns "23"
 rgx("ooo").match("abc123xxx456").isPresent() // returns false;
 // Also throw detailed errors:
 rgx("yyy").match("abc123xxx456").throwIfEmpty()... // throws "Not found pattern 'yyy' in text 'abc123xxx456'"
 rgx("yyy").match("abc123xxx456").get() // throws "Not found pattern 'yyy' in text 'abc123xxx456'"
 rgx("xxx(\\d+)").match("abc123xxx456").group(3) // "throws Match doesn't contain group: 3. Total groups are: 1"
 
See Also:
RgxMatch
  • Constructor Details

    • RgxApi

      public RgxApi()
  • Method Details

    • rgx

      public static Rgx rgx(@Language("regexp") String pattern)
      Usage example:
      
       rgx("\\d(\\d\\d)").match("abc123xxx456").group(1) // returns "123"
       
      Parameters:
      pattern - regular expression
      Returns:
      Rgx wrapper
    • rgx

      public static Rgx rgx(@Language("regexp") String pattern, int flags)

      Use regex with flags

      Usage example:
      
       rgx("ABC", Pattern.CASE_INSENSITIVE).match("abc123XXX456").get() // returns "abc"
       rgx("ABC.+DEF", CASE_INSENSITIVE | DOTALL).match("abc \n def").get() // returns "abc \n def"
       
      Parameters:
      pattern - regular expression
      flags - Regex flags. Same as second argument in: Pattern.compile(String, int)
      Returns:
      Rgx wrapper