The Java.util.regex package is included in jdk1.4 to provide support for regular expressions. And the ReplaceAll and split functions in the Java.lang.String class are also implemented by regular expressions called.
The actions of a regular expression on a string include: string matching, specifying string substitution, specifying string lookup, and string segmentation. Here's an example to illustrate how these operations are implemented:
<%@ page import= "java.util.regex.*"%>
<%
pattern P=null; Regular Expression
Matcher M=null; String
of the action
Boolean B;
String S=null;
StringBuffer Sb=null;
int i=0;
//String match, which is not compliant with the
p = pattern.compile ("A*b");
m = P.matcher ("Baaaaab");
B = m.matches ();
Out.println (b + "<br>");
//String match, which is compliant with the
p = pattern.compile ("A*b");
m = P.matcher ("Aaaaab");
B = m.matches ();
Out.println (b + "<br>");
//String replacement
p = pattern.compile ("ab");
m = P.matcher ("Aaaaab");
s = M.replaceall ("D");
Out.println (s+ "<br>");
p = pattern.compile ("A*b");
m = P.matcher ("Aaaaab");
s = M.replaceall ("D");
out.println (s+ "<br>");
p = pattern.compile ("A*b");
m = P.matcher ("Caaaaab");
s = M.replaceall ("D");
out.println (s+ "<br>");
//String lookup
p = pattern.compile ("cat");
m = P.matcher ("One cat two cats in the Yard");
sb = new StringBuffer ();
while (M.find ()) {
m.appendreplacement (SB, "dog");
i++;
}
M.appendtail (SB);
out.println (sb.tostring () + "<br>");
out.println (i+ "<br>");
i=0;
p = pattern.compile ("cat");
m = P.matcher ("One cat two CA tsi nthe yard");
sb = new StringBuffer ();
while (M.find ()) {
m.appendreplacement (SB, "dog");
i++;
}
M.appendtail (SB);
out.println (sb.tostring () + "<br>");
out.println (i+ "<br>");
p = pattern.compile ("cat");
m = P.matcher ("One cat two cats in the Yard");
P=m.pattern ();
m = P.matcher ("Bacatab");
B = m.matches ();
Out.println (b + "<br>");
s = M.replaceall ("dog");
out.println (s+ "<br>");
i=0;
p = pattern.compile ("(FDS) {2,}");
m = p.matcher ("DSA da Fdsfds Aaafdsafds AAF");
sb = new StringBuffer ();
while (M.find ()) {
m.appendreplacement (SB, "dog");
i++;
}
M.appendtail (SB);
out.println (sb.tostring () + "<br>");
Out.println (i+ "<br>");
p = pattern.compile ("cat");
m = P.matcher ("One cat two cats in the Yard");
sb = new StringBuffer ();
while (M.find ()) {
m.appendreplacement (SB, "<font color=" Red ">cat</font>");
}
M.appendtail (SB);
out.println (sb.tostring () + "<br>");
String aa=sb.tostring ();
out.println (aa+ "<br>");
//String Segmentation
p = pattern.compile ("A +");
string[] A=p.split ("Caaaaaat");
for (i=0;i<a.length;i++)
{
out.println (a[i]+ "<br>");
}
p = pattern.compile ("A +");
a=p.split ("C AA aaaa T", 0);
for (i=0;i<a.length;i++)
{
out.println (a[i]+ "<br>");
}
p = pattern.compile ("+");
a=p.split ("C AA aaaa T", 0);
for (i=0;i<a.length;i++)
{
out.println (a[i]+ "<br>");
}
p = pattern.compile ("\\+");
a=p.split ("Dsafasdfdsafsda+dsagfasdfa+sdafds");
out.println (a.length+ "<br>");
for (i=0;i<a.length;i++)
{
out.println (a[i]+ "<br>");
}
%>