public class FindText { public static boolean match(String pattern,String text) { return match(0,0,pattern,text); } public static boolean match(int ppos,int tpos,String pattern,String text) { System.out.printf(" ==>match(%d,%d,%s,%s)%n",ppos,tpos,pattern,text); if(ppos == pattern.length()) return true; if(tpos == text.length()) { if(ppos+1 == pattern.length() && pattern.charAt(ppos)=='*') return true; return false; } if(pattern.charAt(ppos) == '*') { if(match(ppos+1,tpos,pattern,text)) return true; return match(ppos,tpos+1,pattern,text); } if(pattern.charAt(ppos) == text.charAt(tpos)) return match(ppos+1,tpos+1,pattern,text); return false; } static void test_match(String pattern,String text,boolean res) { boolean b = match(pattern,text); System.out.printf("match(%s,%s)=%s%n",pattern,text,res); assert b == res; } public static void main(String[] args) { test_match("a*b","apple",false); test_match("a*b","apple bear",true); test_match("abc","abc",true); test_match("a*","aa",true); test_match("a*","a",true); } }