Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Java: Regular expressions vs if statements
  2. boolean found = false;
  3.     for (String line: ArrayList){
  4.                 Pattern pattern =
  5.                 Pattern.compile("^test"); //regex
  6.  
  7.                 Matcher matcher =
  8.                 pattern.matcher(line);
  9.  
  10.  
  11.                 while (matcher.find()) {
  12.                     found = true;
  13.                 }
  14.                 if(found){
  15.                     doSomething();
  16.                          }
  17.                     }
  18.                 }
  19.        
  20. for (String line : ArrayList) {
  21.        if (line.startsWith("test"){
  22.             doSomething();
  23.             }
  24.        
  25. for (String line: ArrayList){
  26.     if (line.matches("^test.*") {
  27.         doSomething();
  28.     }
  29. }
  30.        
  31. if (matcher.find()) {
  32.     doSoemthing();
  33. }
  34.        
  35. Pattern pattern = Pattern.compile("^test.*"); //regex
  36.    for (String line: ArrayList){
  37.        if(patter.matches(line)){
  38.             oSomething();
  39.        }
  40.    }