Class pat.Regex
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class pat.Regex

java.lang.Object
   |
   +----pat.RegRes
           |
           +----pat.Regex

public class Regex
extends RegRes
implements FilenameFilter
Shareware: package pat Copyright 1996, Steven R. Brandt

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.
Longer example:
	  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.

Variable Index

 o dontMatchInQuotes
Example of use:
	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
 o esc
esc contains the escape character.
 o ignoreCase
Example of use:
	Regex r = r.Regex("[a-c]");
	r.matchAt("B",0);
	// fails
	r.ignoreCase = true;
	r.matchAt("B",0);
	// succeeds

Constructor Index

 o Regex()
Initializes the object without a Pattern.
 o Regex(Regex)
Essentially clones the Regex object
 o Regex(String)
The String s is automatically compiled for use by search(String)

Method Index

 o accept(File, String)
This method implements FilenameFilter, allowing one to use a Regex to search through a directory using File.list
 o add(Pattern)
add the next Pattern in the chain or set the Pattern if we have null so far
 o clone()
A clone by any other name would smell as sweet.
 o compile(String)
This method tells the Regex object what Pattern to use by future calls to search(String).
 o compile1(StrPos, Rthings)
compile1 compiles one Pattern element, it can be over-ridden to allow the Regex compiler to understand new syntax.
 o matchAt(String, int)
Attempt to match a Pattern beginning at a specified location within the string
 o result()
Return a clone of the underlying RegRes object.
 o search(String)
Search through a Pattern for the first occurrence of a match.
 o searchFrom(String, int)
Search through a Pattern for the first occurence of a match, but start at position
start
 o toString()
Converts the stored Pattern to a String
 o version()
The version of this package

Variables

 o dontMatchInQuotes
  public boolean dontMatchInQuotes
Example of use:
	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
 o ignoreCase
  public boolean ignoreCase
Example of use:
	Regex r = r.Regex("[a-c]");
	r.matchAt("B",0);
	// fails
	r.ignoreCase = true;
	r.matchAt("B",0);
	// succeeds
 o esc
  public char esc
esc contains the escape character. By default, it has the escape character is the backslash, but you can make it anything you want.

Constructors

 o Regex
  public Regex()
Initializes the object without a Pattern. To supply a Pattern use  o Regex
  public Regex(String s)
The String s is automatically compiled for use by search(String)
 o Regex
  public Regex(Regex r)
Essentially clones the Regex object

Methods

 o compile
  public void compile(String pat)
This method tells the Regex object what Pattern to use by future calls to search(String).
 o clone
  public Object clone()
A clone by any other name would smell as sweet.
Overrides:
clone in class RegRes
 o result
  public RegRes result()
Return a clone of the underlying RegRes object.
 o matchAt
  public boolean matchAt(String s,
                         int start_pos)
Attempt to match a Pattern beginning at a specified location within the string
 o search
  public boolean search(String s)
Search through a Pattern for the first occurrence of a match.
 o searchFrom
  public boolean searchFrom(String s,
                            int start)
Search through a Pattern for the first occurence of a match, but start at position
start
 o add
  protected void add(Pattern p2)
add the next Pattern in the chain or set the Pattern if we have null so far
 o compile1
  protected void compile1(StrPos sp,
                          Rthings mk)
compile1 compiles one Pattern element, it can be over-ridden to allow the Regex compiler to understand new syntax. See deriv.java for an example. This routine is the heart of class Regex. Rthings has one integer member called val, it is used to keep track of the number of ()'s in the Pattern.
 o toString
  public String toString()
Converts the stored Pattern to a String
Overrides:
toString in class RegRes
 o accept
  public boolean accept(File dir,
                        String s)
This method implements FilenameFilter, allowing one to use a Regex to search through a directory using File.list
 o version
  public String version()
The version of this package

All Packages  Class Hierarchy  This Package  Previous  Next  Index