Untitled


SUBMITTED BY: itsDaveLad

DATE: March 10, 2016, 3:23 p.m.

FORMAT: Text only

SIZE: 1.3 kB

HITS: 376

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

comments powered by Disqus