正则表达式-匹配横杠-本身

package com.liguogang.utils;

import java.util.regex.Pattern;

public class RegexUtil {

    public static void main(String[] args) {
        String regex = "[0-9]";
        match(1, "匹配数字1", regex, "1");
        match(2, "匹配-字符本身", regex, "-");
        regex = "[0\\-9]";
        match(3, "匹配数字1", regex, "1");
        match(4, "匹配-字符本身", regex, "-");

    }

    // 默认模式
    public static boolean match(int order, String description, String regex, String input) {
        if (null == input) {
            return false;
        }
        boolean matches = Pattern
                .compile(regex)
                .matcher(input)
                .matches();
        System.out.println(String.format("-----------------%s.match %s------------------", order, description));
        System.out.println(String.format("<regex>%s</regex>", regex));
        System.out.println(String.format("<input>%s</input>", input));
        System.out.println(String.format("<matches>%s</matches>", matches));
        return matches;
    }

}

D:\Java\jdk1.8.0_181\bin\java.exe "-javaagent:D:\devTools\idea\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar=52383:D:\devTools\idea\IntelliJ IDEA 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath D:\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\Java\jdk1.8.0_181\lib\tools.jar;E:\project\top\target\classes com.liguogang.utils.RegexUtil
-----------------1.match 匹配数字1------------------
<regex>[0-9]</regex>
<input>1</input>
<matches>true</matches>
-----------------2.match 匹配-字符本身------------------
<regex>[0-9]</regex>
<input>-</input>
<matches>false</matches>
-----------------3.match 匹配数字1------------------
<regex>[0\-9]</regex>
<input>1</input>
<matches>false</matches>
-----------------4.match 匹配-字符本身------------------
<regex>[0\-9]</regex>
<input>-</input>
<matches>true</matches>

Process finished with exit code 0

上一篇:小知识:NFS卡死问题处理


下一篇:NFS共享存储服务