[an error occurred while processing this directive]

Retrieve the String using expression

                         

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

String regex = "Tim (?!Bueneman)[A-Z]\\w+":-Here we have given the expression in the String form.The meaning of the whole expression is to find the name followed by Tim,Let us describe each expression seprately:-

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

\\w:-This character is used to match any alphanumeric character

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

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

NotLookahead.java:-

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

public class NotLookahead {

    public static void main(String[] args) {
        String regex = "Tim (?!Bueneman)[A-Z]\\w+";

        Pattern p = Pattern.compile(regex);

        String text = "I can confirm what Tim Bueneman analyst " +
                "Tim Right is saying — that Amazon.com plans " +
                " a larger-screen model of its e-book player" +
                "Tim Singh aimed at students, in the coming " +
                "Tim Khan also hearing some details about an " +
                " the base model Tim Yadav told is coming in " +
                "though Tim Sharma thinks it may be in October";

        Matcher m = p.matcher(text);
        String s = null;
        while (m.find()) {
            s = m.group();
            System.out.println(s);
        }
    }
}

Output of the program:-

Name starting with Tim from the text are:
Tim Right
Tim Singh
Tim Khan
Tim Yadav
Tim Sharma


DownLoad Source Code

                         

[an error occurred while processing this directive]