Package org.jparsec.pattern
Class Patterns
- java.lang.Object
-
- org.jparsec.pattern.Patterns
-
public final class Patterns extends java.lang.ObjectProvides commonPatternimplementations.
-
-
Field Summary
Fields Modifier and Type Field Description static PatternALWAYSAPatternthat always matches with match length0.static PatternANY_CHARAPatternthat matches any character and only mismatches for an empty string.static PatternDEC_INTEGERAPatternobject that matches a decimal integer, which starts with a non-zero digit and is followed by 0 or more digits.static PatternDECIMALAPatternobject that matches a decimal number that could start with a decimal point or a digit.static PatternEOFAPatternobject that matches if the input has no character left.static PatternESCAPEDAPatternobject that succeeds with match length2if there are at least 2 characters in the input and the first character is'\'.static PatternFRACTIONAPatternobject that matches a decimal point and one or more digits after it.static PatternHEX_INTEGERAPatternobject that matches a hex integer, which starts with a0xor0X, and is followed by one or more hex digits.static PatternINTEGERAPatternobject that matches an integer.static PatternNEVERAPatternthat always returnsPattern.MISMATCH.static PatternOCT_INTEGERAPatternobject that matches an octal integer that starts with a0and is followed by 0 or more[0 - 7]characters.static PatternREGEXP_MODIFIERSAPatternobject that matches regular expression modifiers, which is a list of alpha characters.static PatternREGEXP_PATTERNAPatternobject that matches any regular expression pattern string in the form of/some pattern here/.static PatternSCIENTIFIC_NOTATIONstatic PatternSTRICT_DECIMALAPatternobject that matches a decimal number that has at least one digit before the decimal point.static PatternWORDAPatternobject that matches a standard english word, which starts with either an underscore or an alpha character, followed by 0 or more alphanumeric characters.
-
Constructor Summary
Constructors Modifier Constructor Description privatePatterns()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Patternamong(java.lang.String chars)Returns aPatternobject that matches if the current character in the input is equal to any character inchars, in which case1is returned as match length.static Patternand(Pattern... patterns)Returns aPatternthat matches if all ofpatternsmatches, in which case, the maximum match length is returned.static PatternatLeast(int min, CharPredicate predicate)Returns aPatternobject that matches if the input starts withminor more characters and all satisfypredicate.static PatternatMost(int max, CharPredicate predicate)private static PatterngetModifiersPattern()private static PatterngetRegularExpressionPattern()static PatternhasAtLeast(int n)Returns aPatternobject that matches if the input has at leastncharacters left.static PatternhasExact(int n)Returns aPatternobject that matches if the input has exactlyncharacters left.static PatternisChar(char c)Returns aPatternobject that matches if the current character in the input is equal to characterc, in which case1is returned as match length.static PatternisChar(CharPredicate predicate)Returns aPatternobject that matches if the current character in the input satisfiespredicate, in which case1is returned as match length.static PatternlineComment(java.lang.String begin)Returns aPatternobject that matches a line comment started bybeginand ended byEOForLF(the line feed character).static Patternlonger(Pattern p1, Pattern p2)static Patternlongest(Pattern... patterns)Returns aPatternthat tries all ofpatterns, and picks the one with the longest match length.static Patternmany(int min, CharPredicate predicate)Deprecated.UseatLeast(int, CharPredicate)instead.static Patternmany(CharPredicate predicate)Returns aPatternthat matches 0 or more characters satisfyingpredicate.static Patternmany1(CharPredicate predicate)Returns aPatternthat matches 1 or more characters satisfyingpredicate.private static intmatchMany(CharPredicate predicate, java.lang.CharSequence src, int len, int from, int acc)private static intmatchSome(int max, CharPredicate predicate, java.lang.CharSequence src, int len, int from, int acc)private static intmatchString(java.lang.String str, java.lang.CharSequence src, int begin, int end)Matches (part of) a character sequence against a pattern string.private static intmatchStringCaseInsensitive(java.lang.String str, java.lang.CharSequence src, int begin, int end)(package private) static PatternnextWithEmpty(Pattern left, Pattern right)static Patternnot(Pattern pattern)static PatternnotString(java.lang.String string)Returns aPatternobject that matches if the input has at least 1 character and doesn't matchstring.static PatternnotStringCaseInsensitive(java.lang.String string)Returns aPatternobject that matches if the input has at least 1 character and doesn't matchstringcase insensitively.(package private) static Patternoptional(Pattern pp)static Patternor(Pattern... patterns)Returns aPatternthat matches if any ofpatternsmatches, in which case, the first match length is returned.(package private) static PatternorWithoutEmpty(Pattern left, Pattern right)static Patternrange(char c1, char c2)Returns aPatternobject that matches if the current character in the input is between characterc1andc2, in which case1is returned as match length.static Patternregex(java.lang.String s)Adapts a regular expression pattern string to aPattern.static Patternregex(java.util.regex.Pattern p)Adapts a regular expression pattern to aPattern.static Patternrepeat(int n, CharPredicate predicate)Returns aPatternobject that matches if the input has at leastncharacters and the firstncharacters all satisfypredicate.static Patternsequence(Pattern... patterns)Returns aPatternobject that matches the input againstpatternssequentially.static Patternshorter(Pattern p1, Pattern p2)static Patternshortest(Pattern... patterns)Returns aPatternthat tries all ofpatterns, and picks the one with the shortest match length.static Patternsome(int min, int max, CharPredicate predicate)Deprecated.Usetimes(int, int, CharPredicate)instead.static Patternsome(int max, CharPredicate predicate)Deprecated.UseatMost(int, CharPredicate)instead.static Patternstring(java.lang.String string)Returns aPatternobject that matchesstringliterally.static PatternstringCaseInsensitive(java.lang.String string)Returns aPatternobject that matchesstringcase insensitively.static Patterntimes(int min, int max, CharPredicate predicate)Returns aPatternthat matches at leastminand up tomaxnumber of characters satisfyingpredicate,
-
-
-
Field Detail
-
NEVER
public static final Pattern NEVER
APatternthat always returnsPattern.MISMATCH.
-
ANY_CHAR
public static final Pattern ANY_CHAR
APatternthat matches any character and only mismatches for an empty string.
-
EOF
public static final Pattern EOF
APatternobject that matches if the input has no character left. Match length is0if succeed.
-
ESCAPED
public static final Pattern ESCAPED
APatternobject that succeeds with match length2if there are at least 2 characters in the input and the first character is'\'. Mismatch otherwise.
-
STRICT_DECIMAL
public static final Pattern STRICT_DECIMAL
APatternobject 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.3are all good candidates. While.1, .are not.
-
FRACTION
public static final Pattern FRACTION
APatternobject that matches a decimal point and one or more digits after it.
-
DECIMAL
public static final Pattern DECIMAL
APatternobject that matches a decimal number that could start with a decimal point or a digit.
-
WORD
public static final Pattern WORD
APatternobject 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
APatternobject that matches an octal integer that starts with a0and is followed by 0 or more[0 - 7]characters.
-
DEC_INTEGER
public static final Pattern DEC_INTEGER
APatternobject 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
APatternobject that matches a hex integer, which starts with a0xor0X, and is followed by one or more hex digits.
-
SCIENTIFIC_NOTATION
public static final Pattern SCIENTIFIC_NOTATION
-
REGEXP_PATTERN
public static final Pattern REGEXP_PATTERN
APatternobject that matches any regular expression pattern string in the form of/some pattern here/.'\'is used as escape character.
-
-
Method Detail
-
hasAtLeast
public static Pattern hasAtLeast(int n)
Returns aPatternobject that matches if the input has at leastncharacters left. Match length isnif succeed.
-
hasExact
public static Pattern hasExact(int n)
Returns aPatternobject that matches if the input has exactlyncharacters left. Match length isnif succeed.
-
isChar
public static Pattern isChar(char c)
Returns aPatternobject that matches if the current character in the input is equal to characterc, in which case1is returned as match length. Mismatches otherwise.
-
range
public static Pattern range(char c1, char c2)
Returns aPatternobject that matches if the current character in the input is between characterc1andc2, in which case1is returned as match length.
-
among
public static Pattern among(java.lang.String chars)
Returns aPatternobject that matches if the current character in the input is equal to any character inchars, in which case1is returned as match length.
-
isChar
public static Pattern isChar(CharPredicate predicate)
Returns aPatternobject that matches if the current character in the input satisfiespredicate, in which case1is returned as match length.
-
lineComment
public static Pattern lineComment(java.lang.String begin)
Returns aPatternobject that matches a line comment started bybeginand ended byEOForLF(the line feed character).
-
string
public static Pattern string(java.lang.String string)
Returns aPatternobject that matchesstringliterally.
-
stringCaseInsensitive
public static Pattern stringCaseInsensitive(java.lang.String string)
Returns aPatternobject that matchesstringcase insensitively.
-
notString
public static Pattern notString(java.lang.String string)
Returns aPatternobject that matches if the input has at least 1 character and doesn't matchstring.1is returned as match length if succeeds.
-
notStringCaseInsensitive
public static Pattern notStringCaseInsensitive(java.lang.String string)
Returns aPatternobject that matches if the input has at least 1 character and doesn't matchstringcase insensitively.1is returned as match length if succeeds.
-
not
public static Pattern not(Pattern pattern)
- Parameters:
pattern-- Returns:
- a
Patternthat matches iff the input does not match nestedpattern.
-
and
public static Pattern and(Pattern... patterns)
Returns aPatternthat matches if all ofpatternsmatches, in which case, the maximum match length is returned. Mismatch if any one mismatches.
-
or
public static Pattern or(Pattern... patterns)
Returns aPatternthat matches if any ofpatternsmatches, in which case, the first match length is returned. Mismatch if any one mismatches.
-
sequence
public static Pattern sequence(Pattern... patterns)
Returns aPatternobject that matches the input againstpatternssequentially. Te total match length is returned if all succeed.
-
repeat
public static Pattern repeat(int n, CharPredicate predicate)
Returns aPatternobject that matches if the input has at leastncharacters and the firstncharacters all satisfypredicate.
-
many
@Deprecated public static Pattern many(int min, CharPredicate predicate)
Deprecated.UseatLeast(int, CharPredicate)instead.Returns aPatternobject that matches if the input starts withminor more characters and all satisfypredicate.
-
atLeast
public static Pattern atLeast(int min, CharPredicate predicate)
Returns aPatternobject that matches if the input starts withminor more characters and all satisfypredicate.- Since:
- 2.2
-
many
public static Pattern many(CharPredicate predicate)
Returns aPatternthat matches 0 or more characters satisfyingpredicate.
-
some
@Deprecated public static Pattern some(int min, int max, CharPredicate predicate)
Deprecated.Usetimes(int, int, CharPredicate)instead.Returns aPatternthat matches at leastminand up tomaxnumber of characters satisfyingpredicate,
-
times
public static Pattern times(int min, int max, CharPredicate predicate)
Returns aPatternthat matches at leastminand up tomaxnumber of characters satisfyingpredicate,- Since:
- 2.2
-
some
@Deprecated public static Pattern some(int max, CharPredicate predicate)
Deprecated.UseatMost(int, CharPredicate)instead.
-
atMost
public static Pattern atMost(int max, CharPredicate predicate)
- Since:
- 2.2
-
longer
public static Pattern longer(Pattern p1, Pattern p2)
Returns aPatternthat tries bothp1andp2, and picks the one with the longer match length. If both have the same length,p1is favored.
-
longest
public static Pattern longest(Pattern... patterns)
Returns aPatternthat tries all ofpatterns, 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 aPatternthat tries bothp1andp2, and picks the one with the shorter match length. If both have the same length,p1is favored.
-
shortest
public static Pattern shortest(Pattern... patterns)
Returns aPatternthat tries all ofpatterns, 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 aPatternthat matches 1 or more characters satisfyingpredicate.
-
regex
public static Pattern regex(java.util.regex.Pattern p)
Adapts a regular expression pattern to aPattern.WARNING: in addition to regular expression cost, the returned
Patternobject needs to make a substring copy every time it's evaluated. This can incur excessive copying and memory overhead when parsing large strings. Consider implementingPatternmanually for large input.
-
regex
public static Pattern regex(java.lang.String s)
Adapts a regular expression pattern string to aPattern.WARNING: in addition to regular expression cost, the returned
Patternobject needs to make a substring copy every time it's evaluated. This can incur excessive copying and memory overhead when parsing large strings. Consider implementingPatternmanually for large input.
-
matchSome
private static int matchSome(int max, CharPredicate predicate, java.lang.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, java.lang.CharSequence src, int len, int from, int acc)
-
matchStringCaseInsensitive
private static int matchStringCaseInsensitive(java.lang.String str, java.lang.CharSequence src, int begin, int end)
-
matchString
private static int matchString(java.lang.String str, java.lang.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 fromsrc.end- end of index to scan characters fromsrc.- Returns:
- the number of characters matched, or
Pattern.MISMATCHif an unexpected character is encountered.
-
-