Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Pattern' - 8 code snippet(s) found

 Sample 1. Java Code to get all images url from html code.

regex = Pattern.compile("[http]");
regexMatcher = regex.matcher(htmlParseData.getHtml());
List tr=htmlParseData.getOutgoingUrls();
imgUrlCounter=0;
for(i=0;i<tr.size();i++){
if(tr.get(i).toString().contains(".jpg") || tr.get(i).toString().contains(".jpeg") || tr.get(i).toString().contains(".gif") || tr.get(i).toString().contains(".bmp")){
url = new URL(tr.get(i).toString());
Image image = new ImageIcon(url).getImage();
}
}

   Like      Feedback     regex  pattern  regex.matcher  html  java.util.regex.pattern  regular expressions


 Sample 2. Code to find email ids in a string using Pattern.

String text="vivek boy goy vivek@tech.com viv@ek.com sdjs@adjk";
Pattern regex = Pattern.compile("[@]");
Matcher regexMatcher = regex.matcher(text);
int i =0;
int width = 0;
while (regexMatcher.find()) {
if((regexMatcher.start()-10 > 0) && (regexMatcher.end()+10 < text.length())){
width=10;
String[] substr=text.substring(regexMatcher.start()-width,regexMatcher.end()+width).split(" ");
for(int j=0;j<substr.length;j++){
if(substr[j].contains("@") && (substr[j].contains(".com") || substr[j].contains(".net"))){
System.out.println(substr[j]);
}
}
} else {
width=0;
}
}

   Like      Feedback     regex  pattern  string  regexMatcher.find  java.util.regex.Matcher  java.util.regex.Pattern


 Sample 3. Method to check if the WebUrl matches a Pattern.

public boolean shouldVisit(WebURL url) {
Pattern filters = Pattern.compile(".*(.(htm|html))$");

String href = url.getURL().toLowerCase();
if(filters.matcher(href).matches()){
return true;
}
return false;
}

   Like      Feedback     weburl  patternmatching  .getURL()  pattern  pattern.matcher  pattern.matcher.matches   regex  regular expression


 Sample 4. Initialize object using Builder Pattern

public class Employee {
public String name;
public int age;
public int salary;

Employee withName(String name){
    this.name = name;
    return this;
}

Employee withAge(int age){
    this.age = age;
    return this;
}

Employee withSalary(int salary){
    this.salary = salary;
    return this;
}
}


public class BuilderPatternTest {
    public static void main(String[] args) {
       Employee employee = new Employee().withName("John").withAge(25).withSalary(10000);
    }
}

   Like      Feedback     builder design pattern  builder pattern


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Example of Factory Class

public final class EmployeeFactory {

private Employee svEmp;

public EmployeeFactory(String type){
if( type.equals("Manager")){
svEmp = new Manager();
} else if(type.equals("Developer")){
svEmp = new Developer();
} else if(type.equals("QA")){
svEmp = new QA();
}
}

public Employee getFactoryProduct() {
return svEmp;
}

}

   Like      Feedback     factory design pattern  factory class  final class  composition


 Sample 6. Get Number of instances a pattern exist in the text

public int getNoOfQuestions(){    
   Pattern p = Pattern.compile("Ans.");
Matcher m = p.matcher(text);
int count = 0;
while (m.find()){
   count = count + 1;
}
return count-3;
}

   Like      Feedback     pattern matching  Pattern  Matcher  Matcher.find


 Sample 7. Code Sample / Example / Snippet of java.util.regex.Pattern

    private long getStoreId(File storeFile) {

Pattern p = Pattern.compile(m_name + "-(\d+)");

Matcher m = p.matcher(storeFile.getName());

if (m.find()) {

return Long.parseLong(m.group(1));

}

throw new RuntimeException("Invalid store file name: " + storeFile.getName());

}




   Like      Feedback      java.util.regex.Pattern


 Sample 8. Usage of Builder Class / Builder Pattern

public class BuggyBread {

   private String element1; // Make them private as it supports stronger encapsulation

   private String element2;

   private BuggyBread(String element1, String element2){ // Make it private so that it can only be used by Builder
      this.element1 = element1;
      this.element2 = element2;
   }

   public static class Builder {
      // Create Builder as nested class as its only supposed to Build BuggyBread objects,
      // Make it public so that it can be accessed from outside
      private String element1; // Make them private as it supports stronger encapsulation

      private String element2;

      Builder(){}; // We have to define this constructor if we need overloaded constructor too and need to initialize without arguments too

      Builder(BuggyBread buggybread){ // overloaded constructor to make things easy
         element1 = buggybread.element1;
         element2 = buggybread.element2;
      }

      Builder withElement1(String element1){ // Builder method to either populate elements or override ( if populated through overloaded constructor )
         this.element1 = element1;
         return this;
      }

      Builder withElement2(String element2){
         this.element2 = element2;
         return this;
      }

      BuggyBread build(){ // method to build BuggyBread object using final contents from Builder
         BuggyBread buggybread = new BuggyBread(element1,element2);
         return buggybread;
      }
   }
}

   Like      Feedback     builder pattern  builder class



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus