public class Regex extends RegRes implements FilenameFilter
For the purpose of this documentation, the fact that java interprets the backslash will be ignored. In practice, however, you will need a double backslash to obtain a string that contains a single backslash character. Thus, the example pattern "\b" should really be typed as "\\b" inside java code.
Note that Regex is part of package "com.stevesoft.pat". To use it, simply import com.stevesoft.pat.Regex at the top of your file.
Regex is made with a constructor that takes a String that defines the regular expression. Thus, for example
Regex r = new Regex("[a-c]*");matches any number of characters so long as the are 'a', 'b', or 'c').
To attempt to match the Pattern to a given string, you can use either the search(String) member function, or the matchAt(String,int position) member function. These functions return a boolean which tells you whether or not the thing worked, and sets the methods "charsMatched()" and "matchedFrom()" in the Regex object appropriately.
The portion of the string before the match can be obtained by the left() member, and the portion after the match can be obtained by the right() member.
Essentially, this package implements a syntax that is very much like the perl 5 regular expression syntax. Longer example:
Regex r = new Regex("x(a|b)y"); r.matchAt("xay",0); System.out.println("sub = "+r.stringMatched(1));The above would print "sub = a".
r.left() // would return "x" r.right() // would return "y"
Differences between this package and perl5:
The extended Pattern for setting flags, is now supported,
but the flags are different. "(?i)" tells the pattern to
ignore case, "(?Q)" sets the "dontMatchInQuotes" flag, and
"(?iQ)" sets them both. You can change the escape character.
The pattern
(?e=#)#d+is the same as
\d+, but note that the sequence
(?e=#)must occur at the very beginning of the pattern. There may be other small differences as well. I will either make my package conform or note them as I become aware of them.
This package supports additional patterns not in perl5:
(?@()) | Group | This matches all characters between the '(' character and the balancing ')' character. Thus, it will match "()" as well as "(())". The balancing characters are arbitrary, thus (?@{}) matches on "{}" and "{{}}". |
(?<1) | Backup | Moves the pointer backwards within the text. This allows you to make a "look behind." It fails if it attempts to move to a position before the beginning of the string. "x(?<1)" is equivalent to "(?=x)". The number, 1 in this example, is the number of characters to move backwards. |
Pattern
Modifier and Type | Field and Description |
---|---|
static boolean |
dotDoesntMatchCR
Set this to change the default behavior of the "." pattern.
|
char |
esc
By default,
the escape character is the backslash, but you can
make it anything you want by setting this variable.
|
charsMatched_, didMatch_, marks, matchFrom_, numSubs_, src
Constructor and Description |
---|
Regex()
Initializes the object without a Pattern.
|
Regex(Regex r)
Essentially clones the Regex object
|
Regex(String s)
Create and compile a Regex, but do not throw any exceptions.
|
Regex(String s,
ReplaceRule rp)
Create and compile a Regex, but give it the ReplaceRule
specified.
|
Regex(String s,
String rp)
Create and compile both a Regex and a ReplaceRule.
|
Modifier and Type | Method and Description |
---|---|
boolean |
accept(File dir,
String s)
This method implements FilenameFilter, allowing one
to use a Regex to search through a directory using File.list.
|
protected void |
add(Pattern p2)
Only needed for creating your own extensions of
Regex.
|
Object |
clone()
A clone by any other name would smell as sweet.
|
void |
compile(String prepat)
This method compiles a regular expression, making it
possible to call the search or matchAt methods.
|
protected void |
compile1(StrPos sp,
Rthings mk)
You only need to use this method if you are creating
your own extentions to Regex.
|
patInt |
countMaxChars()
You only need to know about this if you are inventing
your own pattern elements.
|
patInt |
countMinChars()
You only need to know about this if you are inventing
your own pattern elements.
|
static void |
define(String nm,
String pat)
Defines a shorthand for a pattern.
|
static void |
define(String nm,
String pat,
Validator v)
Defines a method to create a new rule.
|
boolean |
equals(Object o)
Returns if this object is considered equal to the other object.
|
static boolean |
getDefaultMFlag()
Get the default value of the m flag.
|
boolean |
getDontMatchInQuotes()
Find out if the dontMatchInQuotes flag is enabled.
|
boolean |
getGFlag()
Get the state of the 'g' flag.
|
boolean |
getIgnoreCase()
Get the state of the ignoreCase flag.
|
boolean |
getMFlag()
Get the state of the sFlag
|
Replacer |
getReplacer() |
ReplaceRule |
getReplaceRule()
Get the current ReplaceRule.
|
boolean |
getSFlag()
Get the state of the sFlag
|
static boolean |
isDefined(String nm)
Test to see if a custom defined rule exists.
|
boolean |
isLiteral()
Checks to see if there are only literal and no special
pattern elements in this Regex.
|
boolean |
matchAt(String s,
int start_pos)
Attempt to match a Pattern beginning
at a specified location within the string.
|
boolean |
matchAt(StringLike s,
int start_pos)
Attempt to match a Pattern beginning
at a specified location within the StringLike.
|
void |
optimize()
Once this method is called, the state of variables
ignoreCase and dontMatchInQuotes should not be changed as the
results will be unpredictable.
|
boolean |
optimized()
This function returns true if the optimize method has
been called.
|
static Regex |
perlCode(String s)
A bit of syntactic surgar for those who want to make
their code look more perl-like.
|
String |
replaceAll(String s)
Replace all occurences of this pattern in String s
according to the ReplaceRule.
|
StringLike |
replaceAll(StringLike s) |
String |
replaceAllFrom(String s,
int pos)
Replace all occurences of this pattern in String s
beginning with position pos according to the ReplaceRule.
|
String |
replaceAllRegion(String s,
int start,
int end)
Replace all occurences of this pattern in String s
beginning with position start and ending with end
according to the ReplaceRule.
|
String |
replaceFirst(String s)
Replace the first occurence of this pattern in String s
according to the ReplaceRule.
|
String |
replaceFirstFrom(String s,
int pos)
Replace the first occurence of this pattern in String s
beginning with position pos according to the ReplaceRule.
|
String |
replaceFirstRegion(String s,
int start,
int end)
Replace the first occurence of this pattern in String s
beginning with position start and ending with end
according to the ReplaceRule.
|
RegRes |
result()
Return a clone of the underlying RegRes object.
|
boolean |
reverseSearch(String s) |
boolean |
reverseSearch(StringLike sl) |
boolean |
search(String s)
Search through a String for the first
occurrence of a match.
|
boolean |
search(StringLike sl) |
boolean |
searchFrom(String s,
int start)
Search through a String for the first
occurence of a match, but start at position start
|
boolean |
searchFrom(StringLike s,
int start) |
boolean |
searchRegion(String s,
int start,
int end)
Search through a region of a String
for the first occurence of a match.
|
static void |
setDefaultMFlag(boolean mFlag)
Set the default value of the m flag.
|
void |
setDontMatchInQuotes(boolean b)
Set the dontMatch in quotes flag.
|
void |
setGFlag(boolean b)
Set the 'g' flag
|
void |
setIgnoreCase(boolean b)
Set the state of the ignoreCase flag.
|
void |
setReplaceRule(ReplaceRule rp)
Change the ReplaceRule of this Regex to rp.
|
void |
setReplaceRule(String rp)
Change the ReplaceRule of this Regex by compiling
a new one using String rp.
|
String |
toString()
Converts the stored Pattern to a String -- this is a
decompile.
|
static void |
undefine(String nm)
Removes a custom defined rule.
|
static String |
version()
The version of this package
|
charsMatched, charsMatched, copyOutOf, didMatch, equals, getString, getStringLike, left, left, matchedFrom, matchedFrom, matchedTo, matchedTo, matchFrom, matchFrom, numSubs, right, right, stringMatched, stringMatched, substring, substring
public char esc
public static boolean dotDoesntMatchCR
public Regex()
compile(java.lang.String)
public Regex(String s)
public Regex(String s, String rp)
ReplaceRule
,
compile(java.lang.String)
public Regex(String s, ReplaceRule rp)
ReplaceRule
,
compile(java.lang.String)
public Regex(Regex r)
public void setDontMatchInQuotes(boolean b)
public boolean getDontMatchInQuotes()
public void setIgnoreCase(boolean b)
public boolean getIgnoreCase()
public static void setDefaultMFlag(boolean mFlag)
public static boolean getDefaultMFlag()
public void setReplaceRule(String rp)
public void setReplaceRule(ReplaceRule rp)
public static boolean isDefined(String nm)
com.stevesoft.pat
public static void undefine(String nm)
com.stevesoft.pat
public static void define(String nm, String pat, Validator v)
public static void define(String nm, String pat)
public ReplaceRule getReplaceRule()
public Replacer getReplacer()
public String replaceFirst(String s)
ReplaceRule
,
getReplaceRule()
public String replaceFirstFrom(String s, int pos)
ReplaceRule
,
getReplaceRule()
public String replaceFirstRegion(String s, int start, int end)
ReplaceRule
,
getReplaceRule()
public String replaceAll(String s)
ReplaceRule
,
getReplaceRule()
public StringLike replaceAll(StringLike s)
public String replaceAllFrom(String s, int pos)
ReplaceRule
,
getReplaceRule()
public String replaceAllRegion(String s, int start, int end)
ReplaceRule
,
getReplaceRule()
public void compile(String prepat) throws RegSyntax
RegSyntax
- is thrown if a syntax error is encountered
in the pattern.
For example, "x{3,1}" or "*a" are not valid
patterns.search(java.lang.String)
,
matchAt(java.lang.String, int)
public boolean equals(Object o)
Object
public Object clone()
public RegRes result()
public boolean matchAt(String s, int start_pos)
search(java.lang.String)
public boolean matchAt(StringLike s, int start_pos)
search(java.lang.String)
public boolean search(String s)
public boolean search(StringLike sl)
public boolean reverseSearch(String s)
public boolean reverseSearch(StringLike sl)
public boolean searchFrom(String s, int start)
start
public boolean searchFrom(StringLike s, int start)
public boolean searchRegion(String s, int start, int end)
public void setGFlag(boolean b)
public boolean getGFlag()
public boolean getSFlag()
public boolean getMFlag()
protected void add(Pattern p2)
protected void compile1(StrPos sp, Rthings mk) throws RegSyntax
RegSyntax
- is thrown when a nonsensensical
pattern is supplied. For example, a pattern beginning
with *.public String toString()
public boolean accept(File dir, String s)
accept
in interface FilenameFilter
dir
- The File
instance for the directory being reads
- The name of the file to testtrue
if the file should be included in the list,
false
otherwise.FileRegex
public static final String version()
public void optimize()
This method will attempt to rewrite your pattern in a way that makes it faster (not all patterns execute at the same speed). In general, "(?: ... )" will be faster than "( ... )" so if you don't need the backreference, you should group using the former pattern.
It will also introduce new pattern elements that you can't get to otherwise, for example if you have a large table of strings, i.e. the months of the year "(January|February|...)" optimize() will make a Hashtable that takes it to the next appropriate pattern element -- eliminating the need for a linear search.
public boolean optimized()
public static Regex perlCode(String s)
Regex r1 = Regex.perlCode("s/hello/goodbye/"); Regex r2 = Regex.perlCode("s'fish'frog'i"); Regex r3 = Regex.perlCode("m'hello');The i for ignoreCase is supported in this syntax, as well as m, s, and x. The g flat is a bit of a special case.
If you wish to replace all occurences of a pattern, you do not put a 'g' in the perlCode, but call Regex's replaceAll method.
If you wish to simply and only do a search for r2's pattern, you can do this by calling the searchFrom method method repeatedly, or by calling search repeatedly if the g flag is set.
Note: Currently perlCode does not support the (?e=#) syntax for changing the escape character.
public boolean isLiteral()
public patInt countMinChars()
public patInt countMaxChars()