|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.knowceans.util.PatternString
public class PatternString
PatternString is a wrapper around pattern matching and substitution
functionality inspired by the Perl a =~ exp
functions.
This implementation puts less emphasis on performance (by putting high priority to reusing Matchers), but is intended for easy use, as most time usually is used for development / porting rather than for operation... The idea is that String-like data can be used for matching operations much more convenient as PatternString objects than by using the final classes String, StringBuffer, Matcher in the Java API.
Further, the methods are organised a bit different from the standard Java API. Basically, there are three operations, find(), match() and substitute(). Unlike their API counterpart find(), which returns a boolean, find() returns this (a substring copy with an empty matcher), in order to allow concatenations. In a subsequent loop, the function found() returns the status of the last find() and operation, and findNext() can be called to advance the parser.
The API functions replaceFirst() and replaceAll() usually return new
instances of the String (as it is immutable). The corresponding substitute(),
however, returns this
and resets the internal matcher if it
was global. For the non-global version, the current state of the matcher,
i.e. its current region is used. Therefore, it is possible to run through a
string using find() or findNext() (findNext() can only be called after
find()) and substitute() [TODO: this hot-needle code must be thoroughly
tested!].
TODO: make pattern string with a constant (and pre-compilable pattern). TODO: fix problems with cascading substitution.
Field Summary | |
---|---|
boolean |
debug
|
Constructor Summary | |
---|---|
PatternString()
Create a empty PatternString. |
|
PatternString(java.lang.String text)
Create a PatternString from the input, with an empty matcher. |
|
PatternString(java.lang.StringBuffer text)
Create a PatternString from the input, with an empty matcher. |
Method Summary | |
---|---|
char |
charAt(int index)
|
void |
configureMatcher(java.util.regex.Pattern p)
sets the matcher with the new pattern |
PatternString |
copy()
copies the pattern string content with an new matcher set to the region of the current one but the matchresult my set, ie., all references to groups information of the last match are kept. |
static PatternString |
create(java.lang.String s)
convenience method to get a pattern string. |
java.lang.StringBuffer |
debugPatternString(java.lang.String pattern)
parses the pattern and outputs the capturing and non-capturing group positions. |
java.lang.String |
debugString()
shows which groups have matched which strings. |
int |
end()
|
int |
end(int group)
|
PatternString |
find(java.lang.String expression)
Emulates a perl find expression like: this =~ /expression/ |
PatternString |
find(java.lang.String expression,
int flags)
emulates a perl find expression like: this =~ /expression/perlFlags |
PatternString |
find(java.lang.String expression,
java.lang.String perlFlags)
emulates a perl find expression like: this =~ /expression/perlFlags |
java.util.Vector<PatternString> |
findAll(java.lang.String expression)
Emulates a repeated perl find like: foreach this =~ /expression/ \@a += \@_ . |
java.util.Vector<PatternString> |
findAll(java.lang.String expression,
java.lang.String perlFlags)
Emulates a repeated perl find like: foreach this =~ /expression/ @@a += @@_ . |
java.util.Vector<PatternString> |
findAll(java.lang.String expression,
java.lang.String replacement,
java.lang.String perlFlags)
Finds all occurrences of the expression and substitutes them with the replacement. |
PatternString |
findNext()
Finds the next occurrence of the pattern and returns it (i.e., return group(0)). |
boolean |
found()
Returns true whether the last find or matching operation has been successful, i.e., the pattern has been found. |
int |
getFlags()
|
java.util.regex.Matcher |
getM()
|
java.util.regex.Matcher |
getMatcher()
|
java.util.regex.Pattern |
getPattern()
|
java.lang.StringBuffer |
getText()
|
java.lang.String |
group()
|
java.lang.String |
group(int number)
return the group with the number after the last match |
int |
groupCount()
|
PatternString |
groupP(int number)
return the group with the number after the last match |
int |
length()
|
static void |
main(java.lang.String[] args)
|
boolean |
match(java.lang.String expression,
int flags)
emulates a perl match expression like: this =~ /expression/perlFlags |
boolean |
match(java.lang.String expression,
java.lang.String perlFlags)
Emulates a perl matching expression like: this =~ m/expression/perlFlags |
boolean |
matched()
Same as found(). |
boolean |
matcherUptodate(java.lang.String expression,
int flags)
Returns whether the matcher is up to date or must be set with new parameters. |
boolean |
nperl(java.lang.String patternCommand)
Like perl, but resets the parser before. |
boolean |
perl(java.lang.String patternCommand)
Perform the command in a perl specification on this and return this, e.g., this =~ s/exp/subs/flags will call substitute(exp,
subs, flags). |
void |
region(int start,
int end)
sets the region for this pattern string |
int |
regionEnd()
return the end of the internal matcher's region |
int |
regionStart()
return the start of the internal matcher's region |
void |
reset()
resets the matcher in order to allow new parsing. |
void |
setFlags(int flags)
|
void |
setM(java.util.regex.Matcher m)
|
void |
setText(java.lang.StringBuffer b)
|
int |
start()
|
int |
start(int group)
|
java.lang.CharSequence |
subSequence(int start,
int end)
|
PatternString |
substitute(java.lang.String expression,
java.lang.String replacement)
emulates a perl substitution expression like: this =~ s/expression/replacement/ |
PatternString |
substitute(java.lang.String expression,
java.lang.String replacement,
int flags,
boolean replaceRemaining)
emulates a perl substitution expression like: this =~ s/expression/replacement/perlFlags |
PatternString |
substitute(java.lang.String expression,
java.lang.String replacement,
java.lang.String perlFlags)
emulates a perl substitution expression like: this =~ s/expression/replacement/perlFlags |
PatternString |
substituteAll(java.lang.String expression,
java.lang.String replacement)
performs global replace of the string expression with the replacement. |
java.lang.String |
toString()
|
int |
translatePerlFlags(java.lang.String perlFlags)
add optional flags g - global, otherwise only first occurrence. |
java.lang.String |
variable(java.lang.String perlVar)
return the variable with the Perl name perlVar, e.g., $_
for last match. |
PatternString |
variablePattern(java.lang.String perlVar)
return the variable with the Perl name perlVar, e.g., $_
for last match. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public boolean debug
Constructor Detail |
---|
public PatternString(java.lang.String text)
text
- public PatternString(java.lang.StringBuffer text)
text
- public PatternString()
s
- Method Detail |
---|
public static void main(java.lang.String[] args)
public PatternString copy()
public static PatternString create(java.lang.String s)
s
-
public boolean nperl(java.lang.String patternCommand)
patternCommand
-
public boolean perl(java.lang.String patternCommand)
this =~ s/exp/subs/flags
will call substitute(exp,
subs, flags).
FIXME: with cascaded substitution, the string of the matcher is always reset to the first value (the field text diverges from the internal state of the matcher).
patternCommand
- -- everything that appears right of a
=~
in Perl, i.e., the expression includes commands
and delimiters. Examples: /abc/
for finding,
s/x(\d+)/u$1/gi
for substituting all x34 or X34
etc. with u34 etc.
public PatternString find(java.lang.String expression, java.lang.String perlFlags)
this =~ /expression/perlFlags
expression
- perlFlags
- (see putPerlFlags)
public PatternString find(java.lang.String expression, int flags)
this =~ /expression/perlFlags
expression
- perlFlags
- Pattern.compile
public PatternString find(java.lang.String expression)
this =~ /expression/
expression
-
public java.util.Vector<PatternString> findAll(java.lang.String expression)
foreach this =~ /expression/ \@a += \@_
.
Returns the array of strings found. After this, found will be false because the search is exhaustive and the matcher of this pattern string is positioned at the end of the last match. Can be used to use find in a Java foreach construct. Use reset() to start at the beginning.
expression
-
public java.util.Vector<PatternString> findAll(java.lang.String expression, java.lang.String perlFlags)
foreach this =~ /expression/ @@a += @@_
. Returns the array of strings found. After this, found will
be false because the search is exhaustive and the matcher of this
pattern string is positioned at the end of the last match. Can be
used to use find in a Java foreach construct. Use reset() to start at
the beginning.
expression
-
public java.util.Vector<PatternString> findAll(java.lang.String expression, java.lang.String replacement, java.lang.String perlFlags)
expression
-
public PatternString findNext()
public boolean found()
public boolean match(java.lang.String expression, java.lang.String perlFlags)
this =~ m/expression/perlFlags
expression
- perlFlags
- (see putPerlFlags)
public boolean match(java.lang.String expression, int flags)
this =~ /expression/perlFlags
expression
- perlFlags
- Pattern.compile
public boolean matched()
public PatternString substitute(java.lang.String expression, java.lang.String replacement, java.lang.String perlFlags)
this =~ s/expression/replacement/perlFlags
expression
- replacement
- perlFlags
- (see putPerlFlags)
public PatternString substitute(java.lang.String expression, java.lang.String replacement, int flags, boolean replaceRemaining)
this =~ s/expression/replacement/perlFlags
expression
- replacement
- perlFlags
- (see Pattern)replaceRemaining
- whether to substitute all remaining occurrences
or only the next one (prior reset() if you want to replace all.)
public PatternString substitute(java.lang.String expression, java.lang.String replacement)
this =~ s/expression/replacement/
expression
- replacement
-
public PatternString substituteAll(java.lang.String expression, java.lang.String replacement)
expression
- replacement
-
public void reset()
public boolean matcherUptodate(java.lang.String expression, int flags)
Note: The expression is checked by reference, i.e., a new instance of the variable expression yields a restarting loop! TODO: check if this makes sense in practice, e.g., with on the fly string concatenations. If not, change == to equals.
expression
- flags
-
public void configureMatcher(java.util.regex.Pattern p)
p
- public java.util.regex.Matcher getMatcher()
public java.util.regex.Pattern getPattern()
public int length()
length
in interface java.lang.CharSequence
public char charAt(int index)
charAt
in interface java.lang.CharSequence
public java.lang.CharSequence subSequence(int start, int end)
subSequence
in interface java.lang.CharSequence
public int translatePerlFlags(java.lang.String perlFlags)
perlFlags
-
public java.lang.String group(int number)
group
in interface java.util.regex.MatchResult
number
-
public PatternString groupP(int number)
number
-
public int regionStart()
public int regionEnd()
public void region(int start, int end)
start
- end
- public int start()
start
in interface java.util.regex.MatchResult
public int end()
end
in interface java.util.regex.MatchResult
public int start(int group)
start
in interface java.util.regex.MatchResult
public int end(int group)
end
in interface java.util.regex.MatchResult
public java.lang.String group()
group
in interface java.util.regex.MatchResult
public int groupCount()
groupCount
in interface java.util.regex.MatchResult
public PatternString variablePattern(java.lang.String perlVar)
$_
for last match.
perlVar
-
public java.lang.String variable(java.lang.String perlVar)
$_
for last match.
perlVar
-
public java.lang.String debugString()
public java.lang.StringBuffer debugPatternString(java.lang.String pattern)
pattern
-
public java.lang.String toString()
toString
in interface java.lang.CharSequence
toString
in class java.lang.Object
public final java.lang.StringBuffer getText()
public final void setText(java.lang.StringBuffer b)
public final int getFlags()
public final void setFlags(int flags)
public final java.util.regex.Matcher getM()
public final void setM(java.util.regex.Matcher m)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |