Using regular Expressions in Java

Source: Internet
Author: User

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>");


}


%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.