Friday, October 9, 2015

The basic regular expression API in Java

The basic regular expression in Java involved 2 classes, java.util.regex.Pattern and  java.util.regex.Matcher. The most basic usage has 3 steps.

  • Create a pattern instance
  • Create a matcher instance from pattern instance. The String  under test is set in this step
  • Check match result using methods from matcher

The following demo code is a hello world level example on how to use Java regular express API.

package com.shengwang.demo;

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

public class HelloRegex {

public static void main(String[] args) {
String input = "Hello World 123#456";
// -------------------------------------------
// Step 1, define Pattern instance
// -------------------------------------------
Pattern p = Pattern.compile("(\\d+)"); // use static method

// -------------------------------------------
// Step 2, define matcher instance
// -------------------------------------------
Matcher m = p.matcher(input); // create matcher from pattern

// -------------------------------------------
// Step3, use loop to go through every match
// -------------------------------------------
while (m.find()) {
System.out.println("" + m.group(1));
}
}
}

In this demo, we use method find()  to do the real match, there are 2 other method matches() and lookingAt(). All these 3 methods return boolean indicates match success or fail. The differences among them are:


  •  matches()  try to match the whole input. 
  • find() try to match a substring of the input.
  • lookingAt() try to match a substring of the input must at beginning of the input.(Think it as a startWith operation)

When using  Pattern + Matcher class, normally means we want to do some manipulation on the matched result. If you only want to get a boolean result to verify a input String, there is no need to use class Pattern + Matcher, use method matches() from String class instead.

  String input = "888ABC999";
boolean matchResult = input.matches("\\d+ABC\\d+"); // match whole string

0 comments:

Post a Comment

Powered by Blogger.

About The Author

My Photo
Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.

Pages

Unordered List