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
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!