Tuesday, July 24, 2007

Patterns and Matchers - java.util.regex Package

Recently I got the oppurtunity to utilize the java.util.regex package, which I had never used before, and found it very simple and handy to work with. As I look to do on my blog, I will share a simple example of how to match character sequences against patterns specified by regular expressions utilizing the Java API. So, as TDD goes we will write the test first and then develop our code against our unit test. If your not familiar with unit testing, you will need to download JUnit. Ok, we will start by creating our new test class.

import junit.framework.TestCase;
import java.util.List;

public class ProcessorTest extends TestCase {
String testData =

public void testProcessor() {
Processor processor = new Processor();
List list = processor.processMessage(testData);
assertEquals("List was not correct size", 2, list.size());
assertEquals("Name did not match", "Chad", list.get(0));
assertEquals("Name did not match", "Bob", list.get(1));

This will obviously fail since we haven't created our Processor class yet, so lets go ahead and create our new Class, and then we can run our unit test to verify that our code is doing what we want it to do.

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.List;
import java.util.ArrayList;

public class Processor {

public List processMessage(String text) {
String pattern1 = <\\w*?name>>;
String pattern2 = </\\w*?name>;
List result = new ArrayList();
Pattern p1 = Pattern.compile(pattern1);
Pattern p2 = Pattern.compile(pattern2);
Matcher m1 = p1.matcher(text);
Matcher m2 = p2.matcher(text);
while ((m1.find()) && (m2.find())) {
result.add(text.substring(m1.end(), m2.start()));
return result;

Now we can run our unit test again and our test will pass. So this is just a simple example of how to use the Matcher and Pattern class provided as part of the JDK, and how to utilize Unit Testing to test your code.

No comments: