HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Using Regular Expressions in J2SE 1.4"
Vol. 7, Issue 1, p. 50

	


Listing 1


import java.io.*;
import java.util.*;
import java.util.regex.*;
public final class Simple {
     public static final void main(String args[]) {
         Pattern p = Pattern.compile("fish");
         Matcher m = p.matcher("one fish, two fish, red fish,
   blue fish");
         int count = 0;
         while (m.find()) {
             count++;
         }
         System.out.println("Count = " + count);
     }
}

Running this program yields:
D:\projects\regex>java simple
Count = 4

Listing 2

D:\projects\regex>java Replace
The cat in the hat

Here's the code itself:
import java.io.*;
import java.util.*;
import java.util.regex.*;
public final class Replace {
     public static final void main(String args[]) {
         Pattern p = Pattern.compile("fish");
         Matcher m = p.matcher("The fish in the hat");
         StringBuffer output = new StringBuffer();
         if (m.find()) {
             m.appendReplacement(output, "cat");
             m.appendTail(output);
         }
         System.out.println(output.toString());
     }
}


Listing 3

import java.io.*;
import java.util.*;
import java.util.regex.*;
public final class ReplaceAll {
     public static final void main(String args[]) {
         Pattern p = Pattern.compile("fish");
         Matcher m = p.matcher("One fish, two fish, red fish,
   blue fish");
         StringBuffer output = new StringBuffer();
         while (m.find()) {
             m.appendReplacement(output, "cat");
         }
         m.appendTail(output);
         System.out.println(output.toString());
     }
}

Listing 4

import java.io.*;
import java.util.*;
import java.util.regex.*;
public final class ShowGroups {
     public static final void main(String args[]) {
        StringBuffer input = new StringBuffer("one if by Java,
  two if by C");
        Pattern p = Pattern.compile("one(.*)two");
        Matcher m = p.matcher(input);
        while (m.find()) {
             for(int i=0;i <= m.groupCount(); i++){
                 System.out.println("Group("+i+") is \"" + m.group(i) + "\"");
             }
         }
     }
}

Listing 5

import java.io.*;
import java.util.*;
import java.util.regex.*;
public final class tagParser {
     public static final void main(String args[]) {
         Pattern p = Pattern.compile("<(.*)>(.*)</\\1>");
         String input = "This is <bold><italic>bold-italic</
   italic></bold>";
         findTag(p,input);
     }

     private static final void findTag(Pattern p, String in) {
         Matcher m = p.matcher(in);
         boolean result = m.find();
         while (result) {
             System.out.println("Found tag: " + m.group(1) + ", inner 
string = " + m.group(2) );
             findTag(p,m.group(2));
             result = m.find();
         }
     }
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.