- java.util.regex.Pattern
- util.regex.Matcher
kulanacagim birkac methodun ne ise yaradigini yaptigina bakalim gerye kalanlarida ileriki zamanda yazacam.
- compile(String kalip) : Bu method araciligi ile istenilen kalip hazirlaniyor.
- matcher(CharSequence text) : Bu method sayasinde hazilanan kalip ile veriyi eslestirmeye yarar.
- find() : yazdigimiz kalip ile veri arasinda eslesme oluyorsa true olmuyorsa false dondurur.
- group() : Bu ise kaliba uygun olanalari alir.
Birinci Adim
public class EmailDemo {
public static void main(String args[]){
String text = "wwwselcukoglu@hotmail.com www.aslan.@hotmail.com www.imanov@.com www.memedov@hotmail. www.tagiyev@hotmail.com www.xaspoladov@hotmail.com";
Pattern pattern = Pattern.compile("www");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
//Console Output
www
www
www
www
www
www
Burada www. noktayi almak icin \\. isareti kullanacam
public class EmailDemo {
public static void main(String args[]){
String text = "wwwselcukoglu@hotmail.com www.aslan.@hotmail.com www.imanov@.com www.memedov@hotmail. www.tagiyev@hotmail.com www.xaspoladov@hotmail.com";
Pattern pattern = Pattern.compile("www\\.");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
//Console Output
www.
www.
www.
www.
www.
www.
Simdide www. ile parcaladigim String degerlerin tamamini gormek icin \\S+isaretini kullanacam
public class EmailDemo {
public static void main(String args[]){
String text = "wwwselcukoglu@hotmail.com www.aslan.@hotmail.com www.imanov@.com www.memedov@hotmail. www.tagiyev@hotmail.com www.xaspoladov@hotmail.com";
Pattern pattern = Pattern.compile("www\\.\\S+");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
//Console Output
www.aslan.@hotmail.com
www.imanov@.com
www.memedov@hotmail.
www.tagiyev@hotmail.com
www.xaspoladov@hotmail.com
Boylece birinci adim tamamlandi ve dikkat ediyorsaniz bu kaliba uymuyanlar yazilmadi mesela wwwselcukoglu@hotmail. 'da www dan sonra nokta olmadigi icin bu kaliba uymadi ve eslestirme sirasinda alinmadi.
Ikinci Adim
@ isaretinin var olup olamadigi ve @ once nokta olmamasini inceliyelim
public class EmailDemo {
public static void main(String args[]){
String text = "wwwselcukoglu@hotmail.com www.aslan.@hotmail.com www.imanov@.com www.memedov@hotmail. www.tagiyev@hotmail.com www.xaspoladov@hotmail.com";
Pattern pattern = Pattern.compile("[^\\.]@\\S+");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
//Console Output
u@hotmail.com
v@.com
v@hotmail.
v@hotmail.com
v@hotmail.com
Burada \\. isret nokta manasinda ve su isaret ise ^ nokta olmayanlar manasina gelir bu olamayanlardan kasit sadece nokta degil istenilen isaretten once ^ bunu koyunce o isret olmayana bakar. ve dikkatle bakarsaniz www.aslan.@hotmail adresinde @ once nokta oldugu icin buda eslestirilme sirasinda alinmadi.
Ucuncu Adim
@------.com seklinde olmasi yani @ten sonra yazi olmali sonra nokta ve sonrada tekrar yazi seklide simdi bunun kalibini olusturalim
public class EmailDemo {
public static void main(String args[]){
String text = "wwwselcukoglu@hotmail.com www.aslan.@hotmail.com www.imanov@.com www.memedov@hotmail. www.tagiyev@hotmail.com www.xaspoladov@hotmail.com";
Pattern pattern = Pattern.compile("@\\S+\\.[a-z]\\S+");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
//Console Output
@hotmail.com
@hotmail.com
@hotmail.com
@hotmail.com
Buradada uymayanlar yine alinmadi mesela www.imanov@.com noktadan once bir yazi olmadigi icin buda alinmadi
Simdi bu yaptiklarimi birlestirince uygulamayi tamalamis oluruz.
public class EmailDemo {
public static void main(String args[]){
String text = "wwwselcukoglu@hotmail.com www.aslan.@hotmail.com www.imanov@.com www.memedov@hotmail. www.tagiyev@hotmail.com www.xaspoladov@hotmail.com";
Pattern pattern = Pattern.compile("((www\\.)\\S+)+([^\\.]@\\S+)+(\\.[a-z]\\S+)");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
//Console Output
www.tagiyev@hotmail.com
www.xaspoladov@hotmail.com
No comments:
Post a Comment