Chapter 13 - Exercise 10

(2) For the phrase "Java now has regular expressions" evaluate whether the following expressions will find a match: `^Java`, `\Breg.*`,`n.w\s+h(a|i)s`,`s?`,`s*`,`s+`,`s{4}`,`S{1}.`,`s{0,3}`.




package com.ciaoshen.thinkinjava.chapter13;
import java.util.*;
import java.util.regex.*;

public class Exercise11 {
    private static final String phrase = "Arline ate eight apples and one orange while Anita hadn't any";
    public static boolean finding(String regex){
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phrase);
        return m.find();
    }
    public static void main(String[] args){
        String regex = "(?i)((^[aeiou])|(\\s+[aeiou]))\\w+?[aeiou]\\b";
        System.out.println(Exercise11.finding(regex));
    }
}



Need Java Developers? Hire Me »

I know Java. Talk is cheap, watch my code.

I live in CANADA now.