All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----pat.RegRes | +----pat.Regex
For the purpose of this documentation, the fact that java interprets the backslash will be ignored. In practise, 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 "pat". To use it, simply import 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 "matchFrom()" 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.
[a-c]matches any character in the range 'a' to 'c'
[a-cde]matches any character in the range 'a' to 'e'
[\-x]matches the '-' character or the 'x' character
[^ab]matches any characters except 'a' or 'b'
.matches any character
{n1,n2}matches between n1 and n2 instances of the previous Pattern, where n1 and n2 are integers.
{n1,}matches at least n1 instances of the previous Pattern
*same as {0,}
?same as {0,1}
+same as {1,}
*?By defuault, {n1,n2}, {n1,}, *, ?, +, match the largest number of occurences of the preceeding Pattern. If any of these is followed by a ? it will attempt, instead, to match the fewest occurents of the preceeding Pattern.
(a|b)matches the character a or b, and returns the matched character as a backreference.
(a)matches the character "a" as a backreference.
Regex r = new Regex("x(a|b)y"); r.matchAt("xay",0); System.out.println("sub = "+r.substring(0));The above would print "sub = a".
r.left() // would return "x" r.right() // would return "y"
.*\bfoo\b\b matches a word boundary. Thus, the example matches "foo" "+foo+", but not "xfoo" or "foox".
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.
This package supports additional patterns:
(?@())This matches all characters between the '(' character and the ')' character, but is smart about it -- see next example
foo(?@[])Matches both "foo[x]" and "foo[x[y]]"
(?< n1)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).
(?< 1)\d\D(?< 1)This Pattern matches a digit/non-digit boundary.
Regex r = r.Regex("a*b"); r.matchAt("a'b'ab",0); // r.charsMatched() now contains 3 r.dontMatchInQuotes = true; r.matchAt("a'b'ab",0); // r.charsMatched() now contains 6
Regex r = r.Regex("[a-c]"); r.matchAt("B",0); // fails r.ignoreCase = true; r.matchAt("B",0); // succeeds
start
public boolean dontMatchInQuotes
Regex r = r.Regex("a*b"); r.matchAt("a'b'ab",0); // r.charsMatched() now contains 3 r.dontMatchInQuotes = true; r.matchAt("a'b'ab",0); // r.charsMatched() now contains 6
public boolean ignoreCase
Regex r = r.Regex("[a-c]"); r.matchAt("B",0); // fails r.ignoreCase = true; r.matchAt("B",0); // succeeds
public char esc
public Regex()
public Regex(String s)
public Regex(Regex r)
public void compile(String pat)
public Object clone()
public RegRes result()
public boolean matchAt(String s, int start_pos)
public boolean search(String s)
public boolean searchFrom(String s, int start)
start
protected void add(Pattern p2)
protected void compile1(StrPos sp, Rthings mk)
public String toString()
public boolean accept(File dir, String s)
public String version()
All Packages Class Hierarchy This Package Previous Next Index