Java - Regular Expression

For developer*

Find Email from string

import java.util.regex.*;

public class Test{
    public static void main(String[] args){
        String s = "*** test@gmail.com&&^ test2@gmail.com((& ";
        Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+").matcher(s);
        while (m.find()) {
            System.out.println(m.group());
        }
    }
}


Find Phone Number from string

ขึ้นต้นด้วย 08 หรือ 09 ตามด้วยตัวเลข 8 หลัก


import java.util.regex.*;
public class Test{
    public static void main(String[] args){
        String s = "kg%$^#keel,ff00933614451ada::%#%^";
        Matcher m = Pattern.compile("0[8|9]{1}[0-9]{8}").matcher(s);
        while (m.find()) {
            System.out.println(m.group());
        }
    }
}

Home Phone from string

ขึ้นต้นด้วย 03 หรือ 02 ตามด้วยตัวเลข  0-9 (7หลัก)





import java.util.regex.*;

public class Test{
    public static void main(String[] args){
        String s = "kg%$^#keel,ff024565543ada::%#%^";
        Matcher m = Pattern.compile("0[3|2]{1}[0-9]{7}").matcher(s);
        while (m.find()) {
            System.out.println(m.group());
        }
    }
}