Class Patterns

java.lang.Object
org.jparsec.pattern.Patterns

public final class Patterns extends Object
Provides common Pattern implementations.
  • Field Details

    • NEVER

      public static final Pattern NEVER
      A Pattern that always returns Pattern.MISMATCH.
    • ALWAYS

      public static final Pattern ALWAYS
      A Pattern that always matches with match length 0.
    • ANY_CHAR

      public static final Pattern ANY_CHAR
      A Pattern that matches any character and only mismatches for an empty string.
    • EOF

      public static final Pattern EOF
      A Pattern object that matches if the input has no character left. Match length is 0 if succeed.
    • ESCAPED

      public static final Pattern ESCAPED
      A Pattern object that succeeds with match length 2 if there are at least 2 characters in the input and the first character is '\'. Mismatch otherwise.
    • INTEGER

      public static final Pattern INTEGER
      A Pattern object that matches an integer.
    • STRICT_DECIMAL

      public static final Pattern STRICT_DECIMAL
      A Pattern object that matches a decimal number that has at least one digit before the decimal point. The decimal point and the numbers to the right are optional.

      0, 11., 2.3 are all good candidates. While .1, . are not.

    • FRACTION

      public static final Pattern FRACTION
      A Pattern object that matches a decimal point and one or more digits after it.
    • DECIMAL

      public static final Pattern DECIMAL
      A Pattern object that matches a decimal number that could start with a decimal point or a digit.
    • WORD

      public static final Pattern WORD
      A Pattern object that matches a standard english word, which starts with either an underscore or an alpha character, followed by 0 or more alphanumeric characters.
    • OCT_INTEGER

      public static final Pattern OCT_INTEGER
      A Pattern object that matches an octal integer that starts with a 0 and is followed by 0 or more [0 - 7] characters.
    • DEC_INTEGER

      public static final Pattern DEC_INTEGER
      A Pattern object that matches a decimal integer, which starts with a non-zero digit and is followed by 0 or more digits.
    • HEX_INTEGER

      public static final Pattern HEX_INTEGER
      A Pattern object that matches a hex integer, which starts with a 0x or 0X, and is followed by one or more hex digits.
    • SCIENTIFIC_NOTATION

      public static final Pattern SCIENTIFIC_NOTATION
      A Pattern object that matches a scientific notation, such as 1e12, 1.2E-1, etc.
    • REGEXP_PATTERN

      public static final Pattern REGEXP_PATTERN
      A Pattern object that matches any regular expression pattern string in the form of /some pattern here/. '\' is used as escape character.
    • REGEXP_MODIFIERS

      public static final Pattern REGEXP_MODIFIERS
      A Pattern object that matches regular expression modifiers, which is a list of alpha characters.
  • Constructor Details

    • Patterns

      private Patterns()
  • Method Details

    • hasAtLeast

      public static Pattern hasAtLeast(int n)
      Returns a Pattern object that matches if the input has at least n characters left. Match length is n if succeed.
    • hasExact

      public static Pattern hasExact(int n)
      Returns a Pattern object that matches if the input has exactly n characters left. Match length is n if succeed.
    • isChar

      public static Pattern isChar(char c)
      Returns a Pattern object that matches if the current character in the input is equal to character c, in which case 1 is returned as match length. Mismatches otherwise.
    • range

      public static Pattern range(char c1, char c2)
      Returns a Pattern object that matches if the current character in the input is between character c1 and c2, in which case 1 is returned as match length.
    • among

      public static Pattern among(String chars)
      Returns a Pattern object that matches if the current character in the input is equal to any character in chars, in which case 1 is returned as match length.
    • isChar

      public static Pattern isChar(CharPredicate predicate)
      Returns a Pattern object that matches if the current character in the input satisfies predicate, in which case 1 is returned as match length.
    • lineComment

      public static Pattern lineComment(String begin)
      Returns a Pattern object that matches a line comment started by begin and ended by EOF or LF (the line feed character).
    • string

      public static Pattern string(String string)
      Returns a Pattern object that matches string literally.
    • stringCaseInsensitive

      public static Pattern stringCaseInsensitive(String string)
      Returns a Pattern object that matches string case insensitively.
    • notString

      public static Pattern notString(String string)
      Returns a Pattern object that matches if the input has at least 1 character and doesn't match string. 1 is returned as match length if succeeds.
    • notStringCaseInsensitive

      public static Pattern notStringCaseInsensitive(String string)
      Returns a Pattern object that matches if the input has at least 1 character and doesn't match string case insensitively. 1 is returned as match length if succeeds.
    • not

      public static Pattern not(Pattern pattern)
      Parameters:
      pattern -
      Returns:
      a Pattern that matches iff the input does not match nested pattern.
    • and

      public static Pattern and(Pattern... patterns)
      Returns a Pattern that matches if all of patterns matches, in which case, the maximum match length is returned. Mismatch if any one mismatches.
    • or

      public static Pattern or(Pattern... patterns)
      Returns a Pattern that matches if any of patterns matches, in which case, the first match length is returned. Mismatch if any one mismatches.
    • orWithoutEmpty

      static Pattern orWithoutEmpty(Pattern left, Pattern right)
    • nextWithEmpty

      static Pattern nextWithEmpty(Pattern left, Pattern right)
    • sequence

      public static Pattern sequence(Pattern... patterns)
      Returns a Pattern object that matches the input against patterns sequentially. Te total match length is returned if all succeed.
    • repeat

      public static Pattern repeat(int n, CharPredicate predicate)
      Returns a Pattern object that matches if the input has at least n characters and the first n characters all satisfy predicate.
    • many

      @Deprecated public static Pattern many(int min, CharPredicate predicate)
      Deprecated.
      Returns a Pattern object that matches if the input starts with min or more characters and all satisfy predicate.
    • atLeast

      public static Pattern atLeast(int min, CharPredicate predicate)
      Returns a Pattern object that matches if the input starts with min or more characters and all satisfy predicate.
      Since:
      2.2
    • many

      public static Pattern many(CharPredicate predicate)
      Returns a Pattern that matches 0 or more characters satisfying predicate.
    • some

      @Deprecated public static Pattern some(int min, int max, CharPredicate predicate)
      Deprecated.
      Returns a Pattern that matches at least min and up to max number of characters satisfying predicate,
    • times

      public static Pattern times(int min, int max, CharPredicate predicate)
      Returns a Pattern that matches at least min and up to max number of characters satisfying predicate,
      Since:
      2.2
    • some

      @Deprecated public static Pattern some(int max, CharPredicate predicate)
      Deprecated.
      Returns a Pattern that matches up to max number of characters satisfying predicate.
    • atMost

      public static Pattern atMost(int max, CharPredicate predicate)
      Returns a Pattern that matches up to max number of characters satisfying predicate.
      Since:
      2.2
    • longer

      public static Pattern longer(Pattern p1, Pattern p2)
      Returns a Pattern that tries both p1 and p2, and picks the one with the longer match length. If both have the same length, p1 is favored.
    • longest

      public static Pattern longest(Pattern... patterns)
      Returns a Pattern that tries all of patterns, and picks the one with the longest match length. If two patterns have the same length, the first one is favored.
    • shorter

      public static Pattern shorter(Pattern p1, Pattern p2)
      Returns a Pattern that tries both p1 and p2, and picks the one with the shorter match length. If both have the same length, p1 is favored.
    • shortest

      public static Pattern shortest(Pattern... patterns)
      Returns a Pattern that tries all of patterns, and picks the one with the shortest match length. If two patterns have the same length, the first one is favored.
    • many1

      public static Pattern many1(CharPredicate predicate)
      Returns a Pattern that matches 1 or more characters satisfying predicate.
    • regex

      public static Pattern regex(Pattern p)
      Adapts a regular expression pattern to a Pattern.

      WARNING: in addition to regular expression cost, the returned Pattern object needs to make a substring copy every time it's evaluated. This can incur excessive copying and memory overhead when parsing large strings. Consider implementing Pattern manually for large input.

    • regex

      public static Pattern regex(String s)
      Adapts a regular expression pattern string to a Pattern.

      WARNING: in addition to regular expression cost, the returned Pattern object needs to make a substring copy every time it's evaluated. This can incur excessive copying and memory overhead when parsing large strings. Consider implementing Pattern manually for large input.

    • optional

      static Pattern optional(Pattern pp)
    • matchSome

      private static int matchSome(int max, CharPredicate predicate, CharSequence src, int len, int from, int acc)
    • getRegularExpressionPattern

      private static Pattern getRegularExpressionPattern()
    • getModifiersPattern

      private static Pattern getModifiersPattern()
    • matchMany

      private static int matchMany(CharPredicate predicate, CharSequence src, int len, int from, int acc)
    • matchStringCaseInsensitive

      private static int matchStringCaseInsensitive(String str, CharSequence src, int begin, int end)
    • matchString

      private static int matchString(String str, CharSequence src, int begin, int end)
      Matches (part of) a character sequence against a pattern string.
      Parameters:
      str - the pattern string.
      src - the input sequence. Must not be null.
      begin - start of index to scan characters from src.
      end - end of index to scan characters from src.
      Returns:
      the number of characters matched, or Pattern.MISMATCH if an unexpected character is encountered.