Java Programing laungage

java.util Projects

java.util Project 1

Capturing Text in a Group in a Regular Expression

In this programming tutorials, we are going to capture the text in a group through the regular expression. Here, we can learn the procedure of capturing text made by the given alphabets from the group of text.

Previous Home Next

Example


package r4r;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class capturetexttest {
	
public static void main(String[] args) throws IOException{
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter text for finding in groups of text: ");
  String inputStr = br.readLine();
  String patStr = "(x(y*))+(z*)";
  Pattern patt = Pattern.compile(patStr);
  Matcher match = patt.matcher(inputStr);
  boolean match1 = match.find();
  if(match1){
  for(int i=0; i<=match.groupCount(); i++){
  String Strgroup = match.group(i);
  System.out.println(Strgroup);
  }
  }
  }
}

Previous Home Next