Galactic Civilizations 3
Parse string with regular expressions in Java

Parse string with regular expressions in Java

Category: Programming.
Posted by on the 22/04/2014.
Last update on the 24/04/2014.

<<Previous tutorial 

Description

This tutorial explains how to parse a string with regular expressions in Java by using the Matcher class.

Introduction

This tutorial can be realized with a simple text editor like « Notepad » or an IDE like « Eclipse » or « Netbeans». This tutorial requires some basic knowledge in Java.

Construct a Matcher object

In order to parse a string, we will use an instance of the Matcher object.

If you want to know how to create the Matcher object, there is a tutorial that explains how to create a regular expression and generate the Matcher object.

Parsing the string

Finally we call the « matches() » method in order to match the regular expression.

This method returns a boolean indicating whether or not the string passed into the matcher() method matches the regular expression.

Here is an example:

public void main(String args[]) {
  String str = "abc007de02abxx3";
  Pattern patt = Pattern.compile("[0-9]+");
  Matcher m = patt.matcher(str);
  if(m.matches()){
    System.out.println("ok");
  }
}

Capturing group

If you need to retrieve the matches or to used capturing groups in the regular expression. You can use the « group() » method of the Matcher. Here is an example:

public void main(String args[]) {
  String str = "abc007de02abxx3";
  Pattern patt = Pattern.compile("[0-9]+");
  Matcher m = patt.matcher(str);
  while(m.matches()){
    System.out.println(m.group);
    // 007 03 3
  }
}

NB: If you use a regex with *, an empty string will be returned.

Here is another example using capturing groups:

public void main(String args[]) {
  String str = "abc007de02abxx3";
  Pattern patt = Pattern.compile("[0-9]+(ab)");
  Matcher m = patt.matcher(str);
  while(m.matches()){
    System.out.println(m.group);
    // 02ab
  }
}

You can also use the « start() » method in order to get the index of the match in the string.

Here is an example:

public void main(String args[]) {
  String str = "abc007de02abxx3";
  Pattern patt = Pattern.compile("[0-9]+");
  Matcher m = patt.matcher(str);
  while(m.matches()){
    System.out.println(m.start);
    // 3814
  }
}

NB: If you use a regex with *, the end of string (\0) will be matched.

Here is an example:

public void main(String args[]) {
  String str = "abc007de02abxx3";
  Pattern patt = Pattern.compile("[0-9]*");
  Matcher m = patt.matcher(str);
  while(m.matches()){
    System.out.println(m.start);
    // 0123456789101112131415
  }
}

You like this tutorial ?
Like this website in order to promote it on facebook !

<<Previous tutorial 

You like this tutorial ? Share it with your friends !
Partager sur Facebook Partager sur Twitter Partager sur Myspace Partager sur Stumbleupon Soumettre sur Reddit Partager sur Digg Ajouter à vos favoris Technorati Ajouter à vos favoris Live Ajouter à vos favoris Google Ajouter sur vos favoris Yahoo