正则表达式基本用法

**

正则表达式:

**
1.作用:
测试字符串内的模式
识别/替换文本
提取文本
Java.util.regex包
主要有两个类:
Pattern正则表达式的编译表示
Compile编译一个正则表达式为Pattern对象

Pattern pattern = Pattern.compile(regex);  //将一个正则表达式的字符串编译成为一个正则表达式对象

Matcher用pattern对象匹配一个字符串,返回匹配结果

Matcher matcher = pattern.matcher(input);  //用过一个正则表达式去丈量输入字符串

Matcher
Index Methods(位置方法) //start(),start(group),end(),end(group)
Study Methods(查找方法) //lookingAt(),find(),find(int start),matches()
Replacement Methods(替换方法)s //replaceAll(String replacement)

\\b:  单词数字边界,数字和字母连在一起不会被分开
public class PatternDemo {

    private static final String regex = "\\bdog\\b";
    private static final String input = "dog dog 1dog2 ,dog] [dog] dogav";

    public static void main(String[] args) {
        //检查字符串里面有多少个dog
        Pattern pattern = Pattern.compile(regex);  //将一个正则表达式的字符串编译成为一个正则表达式对象

        Matcher matcher = pattern.matcher(input);  //用过一个正则表达式去丈量输入字符串
        int count = 0;
        while (matcher.find()){   //find方法返回匹配的字符串,第一次反第一个,第二次返回第二个
            count++;
            System.out.println("Match number:"+count);  //match 英文意思:1.n词:比赛,匹配 2.v词:匹配
            System.out.println("start():"+matcher.start());
            System.out.println("end():"+matcher.end());
        }
    }
}

**

Matcher中的方法:lookingAt()方法 matches()方法

**
lookingAt()方法:部分匹配
matches()方法:完全匹配
正则表达式基本用法_第1张图片

public class LookingAtAndMatches {

    private static final String REGEX = "foo";
    private static final String INPUT = "fooooooo";

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile(REGEX);

        Matcher matcher = pattern.matcher(INPUT);

        System.out.println("Current REGEX is:"+REGEX);
        System.out.println("Current INPUT is:"+INPUT);
        System.out.println("lookingAT():"+matcher.lookingAt());  //部分匹配
        System.out.println("matches():"+matcher.matches());    //完全匹配
    }
}

Matcher中的方法:
appendReplacement(StringBuffer sb, String replacement):替换匹配的字符串
appendTail(StringBuffer sb)://将最后的尾巴字符串附加上

public class RegexDemo {
 
    private static String REGEX = "a*b"; //*表示限定前面的a可以是0个或者是多个
    private static String INPUT = "aabfooaabfooabfoobcdd";
    private static String REPLACE = "-";
 
    public static void main(String[] args) {
        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(INPUT); // get a matcher object
        StringBuffer sb = new StringBuffer();
        //全部替换
        while(m.find()){
            m.appendReplacement(sb,REPLACE);
        }
        //将最后的尾巴字符串附加上
        m.appendTail(sb);   //-foo-foo-foo-
        System.out.println(sb.toString());  //-foo-foo-foo-cdd
    }
}

**

Matcher中的方法:replaceAll(String replacement)

**
replaceAll(String replacement):替换所有匹配项

public class ReplaceDemo {
 
    private static String REGEX = "dog";
    private static String INPUT =
        "The dog says meow. All dogs say meow.";
    private static String REPLACE = "cat";
 
    public static void main(String[] args) {
        Pattern p = Pattern.compile(REGEX);
        // get a matcher object
        Matcher m = p.matcher(INPUT);
        INPUT = m.replaceAll(REPLACE);  //把所有的dog都替换为cat
        System.out.println(INPUT);
    }
}
//这里可以与appendReplacement  +  appendTail 的用法做对比,有各自的优势
public class ReplaceDemo2 {
 
    private static String REGEX = "a*b"; //*表示限定前面的a可以是0个或者是多个
    private static String INPUT =
        "aabfooaabfooabfoobcdef";
    private static String REPLACE = "-";
 
    public static void main(String[] args) {
        Pattern p = Pattern.compile(REGEX);
        // get a matcher object
        Matcher m = p.matcher(INPUT);
        INPUT = m.replaceAll(REPLACE);
        System.out.println(INPUT);  //-foo-foo-foo-cdef
    }
}

**

邮箱格式匹配案例:

**
分析域名部分:

一般域名的规律为“[N级域名][三级域名.]二级域名.顶级域名”,比如“qq.com”、“www.qq.com”、“mp.weixin.qq.com”、“12-34.com.cn”,分析可得域名类似“** .** .** .**”组成。

部分可以表示为[a-zA-Z0-9_-]+ ----》 :**
部分可以表示为.[a-zA-Z0-9_-]+ ----》 :.**
多个“.**”可以表示为(.[a-zA-Z0-9_-]+)+
 综上所述,域名部分可以表示为[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+

最终表达式:
 由于邮箱的基本格式为“名称@域名”,需要使用“^”匹配邮箱的开始部分,用“KaTeX parse error: Can't use function '\.' in math mode at position 74: …[a-zA-Z0-9_-]+(\̲.̲[a-zA-Z0-9_-]+)…

public class EmailTest {
    public static void main(String[] args) {
        regularEmail();
    }
    private static void regularEmail() {
        String REGEX_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*@[A-Za-z0-9]+(([.\\-])[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
        Pattern pattern = Pattern.compile(REGEX_EMAIL);
        String[] emails = new String[]{"123^@qq.com", "[email protected]", "+whatever*[email protected]"};
        for (String email : emails) {
            Matcher matcher = pattern.matcher(email);
            if (matcher.matches()) {
                System.out.println(email + "is valid email"); //"123^@qq.com", "[email protected]"
            } else {
                System.out.println(email + "is not valid email");  //"+whatever*[email protected]"
            }
        }
    }
}
public class Iptest {
    public static void main(String[] args) {
        IpRegular();
    }
    private static void IpRegular() {
        String REGEX_IP = "[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+";
        Pattern pattern = Pattern.compile(REGEX_IP);
        String [] IPS = new String[]{"www.baidu.com","-www.qq.com","+www.163.com","[email protected]"};
        for (String ip : IPS) {
            Matcher matcher = pattern.matcher(ip);
            if (matcher.matches()){
                System.out.println(ip+" :is valid ip!");  //"www.baidu.com","-www.qq.com"
            }else {
                System.out.println(ip+" :is not valid ip!");  //"+www.163.com","[email protected]"
            }
        }
    }
}

你可能感兴趣的:(正则表达式,java)