Quick Start with package pat

There are two sections to the quick start.
  1. If you know perl...

    To use the package within a java file, you must put
    import pat.Regex;
    
    at the top. To create a regular expression matching object type
    	Regex r = new Regex("[a-z]+");
    
    The string argument to the constructor is the regular expression. To see if this regular expression matches anything in a string, do the following:
    	Regex r = new Regex("[a-z]+([0-9])");
    	String m=null,m0=null,s = "some stuff8";
    	if(r.search(s)) {
    		// match succeeded
    		// store result in String m,
    		// in this case it will contain "stuff8"
    		m = r.substring();
    		// save the backreference, the thing in ()'s
    		// in m0.
    		m0 = r.substring(0);
    	} else {
    		// match failed
    	}
    
    That's basically it. You can now get started programming Regex's in java! There are a few other things you will want to know, such as the Check out the main Regex document page if you want to know more.
  2. If you don't know perl...

    You really should learn. Larry Wall has revolutionized computing. Essentially, however, it is a system for easily teaching computers to recognize a pattern. Normally, a character matches itself. Thus, the pattern "a" matches the letter "a". However, a pattern consisting of a series of characters in brackets will match any of those characters. Thus, "[0123456789]" matches any digit. There is a shorthand, however, "[0-9]" does the same job. Thus, "[0-9]" matches "1", "5", etc.

    If you follow a pattern element by "+" then your pattern will match 1 or more instances of the pattern. Thus, "a+" matches "aaaa". The "*" matches 0 or more instances of the previous pattern, "?" matches 0 or 1 instances of the previous pattern.

    Things in parenthesis are grouped: "(and)+" matches "andandand". Moreover, the substring that matched the part of the text in parenthesis can be pulled out in what is called a backreference. This is the thing that substring(0) mentions.

    For more details, see the Regex documentation page. Have fun!


Return to main page