Retrieving String after given Regular expression

This Example describes the way to retrieve the String after giving expression.For this we are going to make program named Lookbehind.java.

Retrieving String after given Regular expression

Retrieving String after given Regular expression

     

This Example describes the way to retrieve the String after giving expression.For this we are going to make program named Lookbehind.java. The steps involved in program Lookbehind.java are described below:-:-

 String regex = "(
?<=http://www.)\\S+":-Here we have given the expression in the String form.Let us describe each expression seprately:-

?:-This character is used to match either one orzero times.

<=:- By this we mean String after "http://www.".

\\S:-Escaped backslash for re.compile.

Overall meaning of this expression is to print rest of the string after the string "http://www.".

Pattern p = Pattern.compile(regex):-Creating a pattern object and compiles the given regular expression into a pattern.

Matcher m = p.matcher(url):-Creates a matcher for matching the given input against pattern p.

Lookbehind.java

 

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

public class Lookbehind {

  public static void main(String[] args) throws Exception {
  String regex = "(?<=http://www.)\\S+";
  
  Pattern p = Pattern.compile(regex);
  String url = "The url of the roseindia is ";
  url += "http://www.roseindia.net. Rohini";
  System.out.println(url);
  
  Matcher m = p.matcher(url);
  while (m.find()) {
  String msg = "Name of the company is : " + m.group();
  System.out.println(msg);
  }
  }
}

Output of the program

The url of the roseindia is http://www.roseindia.net.Rohini
Name of the company is : roseindia.net.

Download Source code