Package org.jparsec.pattern
Class Patterns
- java.lang.Object
-
- org.jparsec.pattern.Patterns
-
public final class Patterns extends java.lang.Object
Provides commonPattern
implementations.
-
-
Field Summary
Fields Modifier and Type Field Description static Pattern
ALWAYS
APattern
that always matches with match length0
.static Pattern
ANY_CHAR
APattern
that matches any character and only mismatches for an empty string.static Pattern
DEC_INTEGER
APattern
object that matches a decimal integer, which starts with a non-zero digit and is followed by 0 or more digits.static Pattern
DECIMAL
APattern
object that matches a decimal number that could start with a decimal point or a digit.static Pattern
EOF
APattern
object that matches if the input has no character left.static Pattern
ESCAPED
APattern
object that succeeds with match length2
if there are at least 2 characters in the input and the first character is'\'
.static Pattern
FRACTION
APattern
object that matches a decimal point and one or more digits after it.static Pattern
HEX_INTEGER
APattern
object that matches a hex integer, which starts with a0x
or0X
, and is followed by one or more hex digits.static Pattern
INTEGER
APattern
object that matches an integer.static Pattern
NEVER
APattern
that always returnsPattern.MISMATCH
.static Pattern
OCT_INTEGER
APattern
object that matches an octal integer that starts with a0
and is followed by 0 or more[0 - 7]
characters.static Pattern
REGEXP_MODIFIERS
APattern
object that matches regular expression modifiers, which is a list of alpha characters.static Pattern
REGEXP_PATTERN
APattern
object that matches any regular expression pattern string in the form of/some pattern here/
.static Pattern
SCIENTIFIC_NOTATION
static Pattern
STRICT_DECIMAL
APattern
object that matches a decimal number that has at least one digit before the decimal point.static Pattern
WORD
APattern
object 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 private
Patterns()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Pattern
among(java.lang.String chars)
Returns aPattern
object that matches if the current character in the input is equal to any character inchars
, in which case1
is returned as match length.static Pattern
and(Pattern... patterns)
Returns aPattern
that matches if all ofpatterns
matches, in which case, the maximum match length is returned.static Pattern
atLeast(int min, CharPredicate predicate)
Returns aPattern
object that matches if the input starts withmin
or more characters and all satisfypredicate
.static Pattern
atMost(int max, CharPredicate predicate)
private static Pattern
getModifiersPattern()
private static Pattern
getRegularExpressionPattern()
static Pattern
hasAtLeast(int n)
Returns aPattern
object that matches if the input has at leastn
characters left.static Pattern
hasExact(int n)
Returns aPattern
object that matches if the input has exactlyn
characters left.static Pattern
isChar(char c)
Returns aPattern
object that matches if the current character in the input is equal to characterc
, in which case1
is returned as match length.static Pattern
isChar(CharPredicate predicate)
Returns aPattern
object that matches if the current character in the input satisfiespredicate
, in which case1
is returned as match length.static Pattern
lineComment(java.lang.String begin)
Returns aPattern
object that matches a line comment started bybegin
and ended byEOF
orLF
(the line feed character).static Pattern
longer(Pattern p1, Pattern p2)
static Pattern
longest(Pattern... patterns)
Returns aPattern
that tries all ofpatterns
, and picks the one with the longest match length.static Pattern
many(int min, CharPredicate predicate)
Deprecated.UseatLeast(int, CharPredicate)
instead.static Pattern
many(CharPredicate predicate)
Returns aPattern
that matches 0 or more characters satisfyingpredicate
.static Pattern
many1(CharPredicate predicate)
Returns aPattern
that matches 1 or more characters satisfyingpredicate
.private static int
matchMany(CharPredicate predicate, java.lang.CharSequence src, int len, int from, int acc)
private static int
matchSome(int max, CharPredicate predicate, java.lang.CharSequence src, int len, int from, int acc)
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.private static int
matchStringCaseInsensitive(java.lang.String str, java.lang.CharSequence src, int begin, int end)
(package private) static Pattern
nextWithEmpty(Pattern left, Pattern right)
static Pattern
not(Pattern pattern)
static Pattern
notString(java.lang.String string)
Returns aPattern
object that matches if the input has at least 1 character and doesn't matchstring
.static Pattern
notStringCaseInsensitive(java.lang.String string)
Returns aPattern
object that matches if the input has at least 1 character and doesn't matchstring
case insensitively.(package private) static Pattern
optional(Pattern pp)
static Pattern
or(Pattern... patterns)
Returns aPattern
that matches if any ofpatterns
matches, in which case, the first match length is returned.(package private) static Pattern
orWithoutEmpty(Pattern left, Pattern right)
static Pattern
range(char c1, char c2)
Returns aPattern
object that matches if the current character in the input is between characterc1
andc2
, in which case1
is returned as match length.static Pattern
regex(java.lang.String s)
Adapts a regular expression pattern string to aPattern
.static Pattern
regex(java.util.regex.Pattern p)
Adapts a regular expression pattern to aPattern
.static Pattern
repeat(int n, CharPredicate predicate)
Returns aPattern
object that matches if the input has at leastn
characters and the firstn
characters all satisfypredicate
.static Pattern
sequence(Pattern... patterns)
Returns aPattern
object that matches the input againstpatterns
sequentially.static Pattern
shorter(Pattern p1, Pattern p2)
static Pattern
shortest(Pattern... patterns)
Returns aPattern
that tries all ofpatterns
, and picks the one with the shortest match length.static Pattern
some(int min, int max, CharPredicate predicate)
Deprecated.Usetimes(int, int, CharPredicate)
instead.static Pattern
some(int max, CharPredicate predicate)
Deprecated.UseatMost(int, CharPredicate)
instead.static Pattern
string(java.lang.String string)
Returns aPattern
object that matchesstring
literally.static Pattern
stringCaseInsensitive(java.lang.String string)
Returns aPattern
object that matchesstring
case insensitively.static Pattern
times(int min, int max, CharPredicate predicate)
Returns aPattern
that matches at leastmin
and up tomax
number of characters satisfyingpredicate
,
-
-
-
Field Detail
-
NEVER
public static final Pattern NEVER
APattern
that always returnsPattern.MISMATCH
.
-
ANY_CHAR
public static final Pattern ANY_CHAR
APattern
that matches any character and only mismatches for an empty string.
-
EOF
public static final Pattern EOF
APattern
object that matches if the input has no character left. Match length is0
if succeed.
-
ESCAPED
public static final Pattern ESCAPED
APattern
object that succeeds with match length2
if there are at least 2 characters in the input and the first character is'\'
. Mismatch otherwise.
-
STRICT_DECIMAL
public static final Pattern STRICT_DECIMAL
APattern
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
APattern
object that matches a decimal point and one or more digits after it.
-
DECIMAL
public static final Pattern DECIMAL
APattern
object that matches a decimal number that could start with a decimal point or a digit.
-
WORD
public static final Pattern WORD
APattern
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
APattern
object that matches an octal integer that starts with a0
and is followed by 0 or more[0 - 7]
characters.
-
DEC_INTEGER
public static final Pattern DEC_INTEGER
APattern
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
APattern
object that matches a hex integer, which starts with a0x
or0X
, 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
APattern
object 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 aPattern
object that matches if the input has at leastn
characters left. Match length isn
if succeed.
-
hasExact
public static Pattern hasExact(int n)
Returns aPattern
object that matches if the input has exactlyn
characters left. Match length isn
if succeed.
-
isChar
public static Pattern isChar(char c)
Returns aPattern
object that matches if the current character in the input is equal to characterc
, in which case1
is returned as match length. Mismatches otherwise.
-
range
public static Pattern range(char c1, char c2)
Returns aPattern
object that matches if the current character in the input is between characterc1
andc2
, in which case1
is returned as match length.
-
among
public static Pattern among(java.lang.String chars)
Returns aPattern
object that matches if the current character in the input is equal to any character inchars
, in which case1
is returned as match length.
-
isChar
public static Pattern isChar(CharPredicate predicate)
Returns aPattern
object that matches if the current character in the input satisfiespredicate
, in which case1
is returned as match length.
-
lineComment
public static Pattern lineComment(java.lang.String begin)
Returns aPattern
object that matches a line comment started bybegin
and ended byEOF
orLF
(the line feed character).
-
string
public static Pattern string(java.lang.String string)
Returns aPattern
object that matchesstring
literally.
-
stringCaseInsensitive
public static Pattern stringCaseInsensitive(java.lang.String string)
Returns aPattern
object that matchesstring
case insensitively.
-
notString
public static Pattern notString(java.lang.String string)
Returns aPattern
object that matches if the input has at least 1 character and doesn't matchstring
.1
is returned as match length if succeeds.
-
notStringCaseInsensitive
public static Pattern notStringCaseInsensitive(java.lang.String string)
Returns aPattern
object that matches if the input has at least 1 character and doesn't matchstring
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 nestedpattern
.
-
and
public static Pattern and(Pattern... patterns)
Returns aPattern
that matches if all ofpatterns
matches, in which case, the maximum match length is returned. Mismatch if any one mismatches.
-
or
public static Pattern or(Pattern... patterns)
Returns aPattern
that matches if any ofpatterns
matches, in which case, the first match length is returned. Mismatch if any one mismatches.
-
sequence
public static Pattern sequence(Pattern... patterns)
Returns aPattern
object that matches the input againstpatterns
sequentially. Te total match length is returned if all succeed.
-
repeat
public static Pattern repeat(int n, CharPredicate predicate)
Returns aPattern
object that matches if the input has at leastn
characters and the firstn
characters all satisfypredicate
.
-
many
@Deprecated public static Pattern many(int min, CharPredicate predicate)
Deprecated.UseatLeast(int, CharPredicate)
instead.Returns aPattern
object that matches if the input starts withmin
or more characters and all satisfypredicate
.
-
atLeast
public static Pattern atLeast(int min, CharPredicate predicate)
Returns aPattern
object that matches if the input starts withmin
or more characters and all satisfypredicate
.- Since:
- 2.2
-
many
public static Pattern many(CharPredicate predicate)
Returns aPattern
that 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 aPattern
that matches at leastmin
and up tomax
number of characters satisfyingpredicate
,
-
times
public static Pattern times(int min, int max, CharPredicate predicate)
Returns aPattern
that matches at leastmin
and up tomax
number 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 aPattern
that tries bothp1
andp2
, 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 aPattern
that 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 aPattern
that tries bothp1
andp2
, 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 aPattern
that 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 aPattern
that 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
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 implementingPattern
manually 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
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 implementingPattern
manually 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.MISMATCH
if an unexpected character is encountered.
-
-