“正则表达式 元字符 字符匹配符”的版本间的差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
| Jihongchang(讨论 | 贡献) | Jihongchang(讨论 | 贡献)  | ||
| (未显示同一用户的10个中间版本) | |||
| 第26行: | 第26行: | ||
| |- | |- | ||
| |. | |. | ||
| − | | | + | |匹配除换行符(\n、\r)之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用像"'''<nowiki>(.|\n)</nowiki>'''"的模式。 | 
| + | 如果要匹配 . 本身,则需要使用 [.](方括号括起来) 或 \\. (斜杠转义) | ||
| |a..b | |a..b | ||
| |以a开头,b结尾,中间包括2个任意字符的长度为4的字符串 | |以a开头,b结尾,中间包括2个任意字符的长度为4的字符串 | ||
| 第44行: | 第45行: | ||
| |- | |- | ||
| |\\w | |\\w | ||
| − | | | + | |匹配字母、数字、下划线。等价于'[A-Za-z0-9_]'。 | 
| |\\d{3}\\w{4} | |\\d{3}\\w{4} | ||
| |以3个数字字符开头的长度为7的数字字母字符串 | |以3个数字字符开头的长度为7的数字字母字符串 | ||
| 第56行: | 第57行: | ||
| |- | |- | ||
| |\\s | |\\s | ||
| − | | | + | |匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。 | 
| + | | | ||
| + | | | ||
| + | | | ||
| + | |- | ||
| + | |\\S | ||
| + | |匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 | ||
| | | | | ||
| | | | | ||
| 第62行: | 第69行: | ||
| |} | |} | ||
| − | + | 字符匹配符匹配单个字符 | |
| 第703行: | 第710行: | ||
| 找到:! | 找到:! | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | === 示例:匹配除 \n 之外的所有字符,如果要匹配 . 本身则需要使用 \\. === | ||
| + | <syntaxhighlight lang="java"> | ||
| + | import java.util.regex.Matcher; | ||
| + | import java.util.regex.Pattern; | ||
| + | |||
| + | public class RegExp03 { | ||
| + | |||
| + |     public static void main(String[] args) { | ||
| + | |||
| + |         String content = "a1 1c_8\na bc    ABC@,.!"; | ||
| + | |||
| + |         String regStr = "."; //匹配 \n 之外的所有字符 | ||
| + | |||
| + |         Pattern pattern = Pattern.compile(regStr); | ||
| + | |||
| + |         Matcher matcher = pattern.matcher(content); | ||
| + | |||
| + |         while (matcher.find()) { | ||
| + | |||
| + |             System.out.println("找到:" + matcher.group(0)); | ||
| + | |||
| + |         } | ||
| + | |||
| + |     } | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight><syntaxhighlight lang="console"> | ||
| + | 找到:a | ||
| + | 找到:1 | ||
| + | 找到:  | ||
| + | 找到:1 | ||
| + | 找到:c | ||
| + | 找到:_ | ||
| + | 找到:8 | ||
| + | 找到:a | ||
| + | 找到:  | ||
| + | 找到:b | ||
| + | 找到:c | ||
| + | 找到:  | ||
| + | 找到:  | ||
| + | 找到:  | ||
| + | 找到:  | ||
| + | 找到:A | ||
| + | 找到:B | ||
| + | 找到:C | ||
| + | 找到:@ | ||
| + | 找到:, | ||
| + | 找到:. | ||
| + | 找到:! | ||
| + | |||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | |||
| + | === 示例:\ 匹配 超链接中的 \ === | ||
| + | <syntaxhighlight lang="java"> | ||
| + | import java.util.regex.Matcher; | ||
| + | import java.util.regex.Pattern; | ||
| + | |||
| + | public class RegExp03 { | ||
| + | |||
| + |     public static void main(String[] args) { | ||
| + | |||
| + |         String content = "http://www.jihongchang.top/index.php/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"; | ||
| + | |||
| + |         String regStr = "/"; //匹配 / | ||
| + | |||
| + |         Pattern pattern = Pattern.compile(regStr); | ||
| + | |||
| + |         Matcher matcher = pattern.matcher(content); | ||
| + | |||
| + |         while (matcher.find()) { | ||
| + | |||
| + |             System.out.println("找到:" + matcher.group(0)); | ||
| + | |||
| + |         } | ||
| + | |||
| + |     } | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight><syntaxhighlight lang="console"> | ||
| + | 找到:/ | ||
| + | 找到:/ | ||
| + | 找到:/ | ||
| + | 找到:/ | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | === 示例:\\\\ 匹配 \  本地磁盘路径分隔符 === | ||
| + | <syntaxhighlight lang="java"> | ||
| + | import java.util.regex.Matcher; | ||
| + | import java.util.regex.Pattern; | ||
| + | |||
| + | public class RegExp03 { | ||
| + | |||
| + |     public static void main(String[] args) { | ||
| + | |||
| + |         String content = "C:\\Users\\Administrator\\Desktop\\常用"; | ||
| + | |||
| + |         String regStr = "\\\\"; | ||
| + | |||
| + |         Pattern pattern = Pattern.compile(regStr); | ||
| + | |||
| + |         Matcher matcher = pattern.matcher(content); | ||
| + | |||
| + |         while (matcher.find()) { | ||
| + | |||
| + |             System.out.println("找到:" + matcher.group(0)); | ||
| + | |||
| + |         } | ||
| + | |||
| + |     } | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight><syntaxhighlight lang="console"> | ||
| + | 找到:\ | ||
| + | 找到:\ | ||
| + | 找到:\ | ||
| + | 找到:\ | ||
| + | |||
| + | </syntaxhighlight><syntaxhighlight lang="java"> | ||
| + | import java.io.File; | ||
| + | import java.util.regex.Matcher; | ||
| + | import java.util.regex.Pattern; | ||
| + | |||
| + | public class RegExp03 { | ||
| + | |||
| + |     public static void main(String[] args) { | ||
| + | |||
| + |         File f = new File("."); | ||
| + | |||
| + |         String absolutePath = f.getAbsolutePath(); | ||
| + | |||
| + |         System.out.printf("absolutePath:%s\n", absolutePath); | ||
| + | |||
| + |         String regStr = "\\\\"; //匹配 / | ||
| + | |||
| + |         Pattern pattern = Pattern.compile(regStr); | ||
| + | |||
| + |         Matcher matcher = pattern.matcher(absolutePath); | ||
| + | |||
| + |         while (matcher.find()) { | ||
| + | |||
| + |             System.out.println("找到:" + matcher.group(0)); | ||
| + | |||
| + |         } | ||
| + | |||
| + |     } | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight><syntaxhighlight lang="console"> | ||
| + | absolutePath:E:\record\2022\8\29\untitled\. | ||
| + | 找到:\ | ||
| + | 找到:\ | ||
| + | 找到:\ | ||
| + | 找到:\ | ||
| + | 找到:\ | ||
| + | 找到:\ | ||
| </syntaxhighlight> | </syntaxhighlight> | ||
2022年11月20日 (日) 17:37的最新版本
https://www.bilibili.com/video/BV1Eq4y1E79W/?p=7
| 符号 | 符号 | 示例 | 解释 | 匹配输入 | 
|---|---|---|---|---|
| [] | 可接收的字符列表 | [efgh] | e、f、g、h中的任意1个字符 | |
| [^] | 不接收的字符列表 | [^abc] | 除a、b、c之外的任意1个字符,包括数组和特殊符号 | |
| - | 连字符 | A-Z | 任意单个大写字母 | |
| . | 匹配除换行符(\n、\r)之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用像"(.|\n)"的模式。 如果要匹配 . 本身,则需要使用 [.](方括号括起来) 或 \\. (斜杠转义) | a..b | 以a开头,b结尾,中间包括2个任意字符的长度为4的字符串 | aaab、aefb、a35b、a#*b | 
| \\d | 匹配单个数字字符,相当于[0-9] | \\d{3}(\\d)? | 包含3个或4个数字的字符串 | 123、9876 | 
| \\D | 匹配单个非数字字符,相当于[^0-9] | \\D(\\d)* | 以单个非数字字符开头,后接任意个数字字符串 | a、A342 | 
| \\w | 匹配字母、数字、下划线。等价于'[A-Za-z0-9_]'。 | \\d{3}\\w{4} | 以3个数字字符开头的长度为7的数字字母字符串 | 234abcd、12345Pe | 
| \\W | 匹配单个非数字、大小写字母字符,相当于[^0-9a-zA-Z_] | \\W+\\d{2} | 以至少1个非数字字母字符开头,2个数字字符结尾的字符串 | #29、#?@10 | 
| \\s | 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。 | |||
| \\S | 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 | 
字符匹配符匹配单个字符
示例:\\d\\d\\d 等价与 \\d{3}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 演示转义字符的使用
 */
public class RegExp02 {
    public static void main(String[] args) {
        String content = "abc.$)123";
        //String regStr = "\\d\\d\\d";
        String regStr = "\\d{3}";
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:123
https://www.bilibili.com/video/BV1Eq4y1E79W?p=8
示例:匹配 a-z 之间任意1个字符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8";
        String regStr = "[a-z]"; //匹配 a-z 之间任意1个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:a
找到:c
示例:匹配 b-z 之间任意1个字符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8";
        String regStr = "[b-z]"; //匹配 b-z 之间任意1个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:c
只要连接符“-”的 from < to,就可以
示例:匹配 A-Z 之间任意1个字符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8";
        String regStr = "[A-Z]"; //匹配 A-Z 之间任意1个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
控制台输出为空
示例:匹配 A-z 之间任意1个字符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "Aa11c8z";
        String regStr = "[A-z]"; //匹配 A-z 之间任意1个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:A
找到:a
找到:c
找到:z
Java正则表达式默认区分字母大小写,如何实现不区分大小写?
- (?i)abc 表示abc都不区分大小写
- a(?i)bc 表示bc不区分大小写
- a((?i)b)c 表示只有b不区分大小写
- Pattern pattern = Pattern.compile(regStr, Pattern.CASE_INSENSITIVE);
示例:默认区分大小写
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8abcABC";
        String regStr = "abc"; //匹配 abc 字符串[默认区分大小写]
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:abc
示例:(?i)abc
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8abcABC";
        String regStr = "(?i)abc"; //匹配 abc 字符串[不区分大小写]
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:abc
找到:ABC
示例:Pattern.compile(regStr, Pattern.CASE_INSENSITIVE)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8abcABC";
        String regStr = "abc";
        //当创建 Pattern 对象时,指定 Pattern.CASE_INSENSITIVE,表示匹配是不区分字母大小写的
        Pattern pattern = Pattern.compile(regStr, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:abc
找到:ABC
示例:[0-9]
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8abcABC";
        String regStr = "[0-9]"; //匹配 0-9 之间任意一个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:1
找到:1
找到:8
示例:[^a-z]
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8abcABC";
        String regStr = "[^a-z]"; //匹配不在 a-z 之间任意一个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:1
找到:1
找到:8
找到:A
找到:B
找到:C
示例:[^0-9]
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8abcABC";
        String regStr = "[^0-9]"; //匹配不在 0-9 之间任意一个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:a
找到:c
找到:a
找到:b
找到:c
找到:A
找到:B
找到:C
https://www.bilibili.com/video/BV1Eq4y1E79W?p=9
示例:[abcd]表示可以匹配abcd中的任意一个字符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8abcABCy";
        String regStr = "[abcd]"; //匹配在 abcd 中的任意一个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:a
找到:c
找到:a
找到:b
找到:c
示例:\\D 表示可以匹配不在 0-9 范围内的任意1个字符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c8abcABC";
        String regStr = "\\D"; //匹配不在 0-9 范围内的任意一个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:a
找到:c
找到:a
找到:b
找到:c
找到:A
找到:B
找到:C
示例:\w 表示匹配单个数字、大小写字母字符,相当于[0-9a-zA-Z]
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c_8abcABC@,.!";
        String regStr = "\\w"; //匹配单个数字、大小写字母字符,相当于[0-9a-zA-Z]
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:a
找到:1
找到:1
找到:c
找到:_
找到:8
找到:a
找到:b
找到:c
找到:A
找到:B
找到:C
注意:_ 也会匹配出来
示例:\\W 相当于[^a-zA-Z0-9],与 \\w 刚好相反
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a11c_8abcABC@,.!";
        String regStr = "\\W"; //效果与[^a-zA-Z0-9_]相同,与 \\w 刚好相反
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:@
找到:,
找到:.
找到:!
示例:\\s 匹配空格、制表符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a1 1c_8a bc    ABC@,.!";
        String regStr = "\\s"; //匹配空格、制表符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到: 
找到: 
找到: 
找到: 
找到: 
找到:
注意:几个“找到:”要看制表符有没有在IDE配置里设置4个空格代替,要看被匹配内容的字符串字面常量里的是什么
示例:\\S 匹配任何非空白字符,和 \\s 刚好相反
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a1 1c_8a bc    ABC@,.!";
        String regStr = "\\S"; //匹配任何空格、制表符以外的任意1个字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:a
找到:1
找到:1
找到:c
找到:_
找到:8
找到:a
找到:b
找到:c
找到:A
找到:B
找到:C
找到:@
找到:,
找到:.
找到:!
示例:匹配除 \n 之外的所有字符,如果要匹配 . 本身则需要使用 \\.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "a1 1c_8\na bc    ABC@,.!";
        String regStr = "."; //匹配 \n 之外的所有字符
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:a
找到:1
找到: 
找到:1
找到:c
找到:_
找到:8
找到:a
找到: 
找到:b
找到:c
找到: 
找到: 
找到: 
找到: 
找到:A
找到:B
找到:C
找到:@
找到:,
找到:.
找到:!
示例:\ 匹配 超链接中的 \
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "http://www.jihongchang.top/index.php/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F";
        String regStr = "/"; //匹配 /
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:/
找到:/
找到:/
找到:/
示例:\\\\ 匹配 \ 本地磁盘路径分隔符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        String content = "C:\\Users\\Administrator\\Desktop\\常用";
        String regStr = "\\\\";
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
找到:\
找到:\
找到:\
找到:\
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp03 {
    public static void main(String[] args) {
        File f = new File(".");
        String absolutePath = f.getAbsolutePath();
        System.out.printf("absolutePath:%s\n", absolutePath);
        String regStr = "\\\\"; //匹配 /
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(absolutePath);
        while (matcher.find()) {
            System.out.println("找到:" + matcher.group(0));
        }
    }
}
absolutePath:E:\record\2022\8\29\untitled\.
找到:\
找到:\
找到:\
找到:\
找到:\
找到:\