Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

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.

Ads

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

Advertisements

Share on Google+Share on Google+

Retrieving String after given Regular expression

Posted on: August 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Retrieving String after given Regular expression  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads