|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objecttcl.lang.Regex
public class Regex
The Regex class can be used to match a TCL-style regular expression against a string and optionally replace the matched parts with new strings. It serves as a gasket between TCL regular expression style and java.util.regex.* style. Known problems: FIXME - Most important: TCL always attempts to match the longest string starting from the outermost levels to the inner levels of parens. With alternation (|) TCL chooses the longest match of all the branches. Java, on the other hand, evaluates the RE from left to right, and returns the first successful match, even if it's not the longest. This class follows the Java rules, because there doesn't appear to be a way to influence Matcher's behavior to choose the longest. Probably the real solution is to write a custom regex engine that performs according to TCL rules. - BRE's are not supported (embedded option 'b' causes PatternSyntaxException) - ERE's are not supported, unless they are 'ARE'-compatible and are not explicitly requested with the 'e' embedded option ('e' causes PatternSyntaxException) - getInfo(), used by 'regexp -about', doesn't provide flag information. But the test cases in reg.test that use 'regexp -about', and the behavior during 'regexp -about' compile errors is adjusted. - Some syntax errors that would occur in C TCL don't occur here because Java is more forgiving of bad RE syntax
Matcher
,
Pattern
Field Summary | |
---|---|
static int |
TCL_REG_ADVANCED
AREs (also EREs) flag |
static int |
TCL_REG_ADVF
advanced features in ARE flag |
static int |
TCL_REG_BASIC
BRE flag (convenience) |
static int |
TCL_REG_CANMATCH
report details on partial/limited matches flag |
static int |
TCL_REG_EXPANDED
Expanded - comments and whitespace flag |
static int |
TCL_REG_EXTENDED
ERE flag |
static int |
TCL_REG_NEWLINE
Newlines are line terminators flag |
static int |
TCL_REG_NLANCH
^ matches after \n $ before flag |
static int |
TCL_REG_NLSTOP
\n doesn't match . |
static int |
TCL_REG_NOCASE
ignore case flag |
static int |
TCL_REG_NOSUB
don't care about subexpressions flag |
static int |
TCL_REG_QUOTE
regex is a literal flag |
Constructor Summary | |
---|---|
Regex(String regexp,
String string,
int offset)
Stores params in object and compiles given regexp. |
|
Regex(String regexp,
String string,
int offset,
int flags)
Stores params in object and compiles given regexp. |
|
Regex(String regexp,
String string,
int offset,
int flags,
String xflags)
Stores params in object and compiles given regexp. |
Method Summary | |
---|---|
protected Pattern |
compile(String tclRegex)
Rewrite TCL regex into a Java regex, and compiles it to a Java Pattern. |
int |
end()
|
int |
end(int group)
|
int |
getCount()
|
TclObject |
getInfo(Interp interp)
Returns a list containing information about the regular expression. |
protected int |
getJavaFlags()
Convert this.flags to Java's Pattern flags |
int |
getOffset()
|
static String |
getPatternSyntaxMessage(PatternSyntaxException ex)
Return a regexp pattern syntax error message in a format expected by Tcl, primarily to make TCL tests to pass. |
String |
group()
|
String |
group(int group)
Returns the input subsequence captured by the given group during the previous match operation. |
int |
groupCount()
|
boolean |
match()
Attempts to match the input string against the regular expression. |
protected int |
parseFlagString(String optionString,
boolean isEmbed)
Parse up a embedded options string or an xflags string and modify this.flags. |
protected static String |
parseSubSpec(String subSpec)
------------------------------------------------------------------------ ----- parseSubSpec -- Parses the replacement string (subSpec param) which is in Tcl's form. |
String |
replace(String tclSubSpec,
boolean all)
Replaces the subsequence(s) of the input sequence that match the pattern with the given TCL-style replacement string. |
int |
start()
|
int |
start(int group)
|
protected void |
testForUnsupportedFlags()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int TCL_REG_BASIC
public static final int TCL_REG_EXTENDED
public static final int TCL_REG_ADVF
public static final int TCL_REG_ADVANCED
public static final int TCL_REG_QUOTE
public static final int TCL_REG_NOCASE
public static final int TCL_REG_NOSUB
public static final int TCL_REG_EXPANDED
public static final int TCL_REG_NLSTOP
public static final int TCL_REG_NLANCH
public static final int TCL_REG_NEWLINE
public static final int TCL_REG_CANMATCH
Constructor Detail |
---|
public Regex(String regexp, String string, int offset, int flags) throws PatternSyntaxException
regexp
- TCL-style regular expressionstring
- input stringoffset
- offset of the input string where matching startsflags
- Regex.TCL_REG_* flags of pattern object that compiles regexp
PatternSyntaxException
- when there is an error during regexp compilationpublic Regex(String regexp, String string, int offset) throws PatternSyntaxException
regexp
- TCL-style regular expressionstring
- input stringoffset
- offset of the input string where matching starts
PatternSyntaxException
- when there is an error during regexp compilationpublic Regex(String regexp, String string, int offset, int flags, String xflags) throws PatternSyntaxException
regexp
- TCL-style regular expressionstring
- input stringoffset
- offset of the input string where matching startsflags
- Regex.TCL_REG_* flags of pattern object that compiles regexpxflags
- Flag string from reg.test (for testregexp)
PatternSyntaxException
- when there is an error during regexp compilationMethod Detail |
---|
public boolean match()
public String replace(String tclSubSpec, boolean all)
tclSubSpec
- TCL-regsub-style replacement stringall
- If true, all matches are replaced. If false, only the
first match is replaced
public TclObject getInfo(Interp interp) throws TclException
interp
- current Jacl interpreter object
TclException
protected static String parseSubSpec(String subSpec)
subSpec
- The replacement string
public int groupCount()
Matcher.groupCount()
public int start()
Matcher.start()
public int start(int group)
group
- The index of a capturing group in this matcher's pattern
Matcher.start(int)
public int end()
Matcher.end()
public int end(int group)
group
- The index of a capturing group in this matcher's pattern
Matcher.end(int)
public String group()
public String group(int group)
group
- The index of a capturing group in this matcher's pattern
Matcher.group(int)
public int getCount()
public int getOffset()
public static String getPatternSyntaxMessage(PatternSyntaxException ex)
ex
- A PatternSyntaxException thrown from the Regex constructor
protected int getJavaFlags()
protected int parseFlagString(String optionString, boolean isEmbed) throws PatternSyntaxException
optionString
- contains embedded options or xflagsisEmbed
- if true, string will be ignored
if it does not start with (?,
and anything after ')' will be ignored.
PatternSyntaxException
- if flag string contains unknown or
unsupported flagsprotected void testForUnsupportedFlags() throws PatternSyntaxException
PatternSyntaxException
- if a flag is unsupportedprotected Pattern compile(String tclRegex) throws PatternSyntaxException
tclRegex
- The TCL regular expression to be compiled
PatternSyntaxException
- if the expression's syntax is invalid
or unsupportedRegex
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |