package mojijavatutoriali; import java.util.regex.*; /** * Created by Uporabnik on 10. 03. 2016. */ public class java20 { public static void main(String args[]){ String longString = " Derek Banas CA 12345 PA (412)555-1212 johnsmith@hotmail.com 412-555-1234 412 555-1234 "; String strangeString = " 1Z aaa **** *** {{{ {{ { "; /* [ ] Insert characters that are valid [^ ] Insert characters that are not valid \\s Any white space \\S Any non white space {n,m} Whatever proceeds must occur between n and m times */ // Word must contain letters that are 2 to 20 characters in length // [A-Za-z]{2,20} 0r \w{2,20} regexChecker("\\s[A-Za-z]{2,20}\\s",longString); } public static void regexChecker(String theRegex, String str2Check){ Pattern checkRegex = Pattern.compile(theRegex); Matcher regexmatcher = checkRegex.matcher(str2Check); while(regexmatcher.find()){ if(regexmatcher.group().length()!=0){ System.out.println(regexmatcher.group().trim()); } System.out.println("Start index: " + regexmatcher.start()); System.out.println("End index: " + regexmatcher.end()); } } }