“正则表达式方括号的应用”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“https://www.bilibili.com/video/BV1Eq4y1E79W/?p=17 16:50”的新页面)
 
第1行: 第1行:
 
https://www.bilibili.com/video/BV1Eq4y1E79W/?p=17  16:50
 
https://www.bilibili.com/video/BV1Eq4y1E79W/?p=17  16:50
 +
 +
=== 示例:普通的 . ===
 +
<syntaxhighlight lang="java">
 +
import java.util.regex.Matcher;
 +
import java.util.regex.Pattern;
 +
 +
public class TestTemp {
 +
 +
    public static void main(String[] args) {
 +
 +
        String content = "hello abc 111";
 +
 +
        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">
 +
找到:h
 +
找到:e
 +
找到:l
 +
找到:l
 +
找到:o
 +
找到:
 +
找到:a
 +
找到:b
 +
找到:c
 +
找到:
 +
找到:1
 +
找到:1
 +
找到:1
 +
 +
</syntaxhighlight>

2022年11月19日 (六) 08:16的版本

https://www.bilibili.com/video/BV1Eq4y1E79W/?p=17 16:50

示例:普通的 .

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestTemp {

    public static void main(String[] args) {

        String content = "hello abc 111";

        String regStr = "."; //匹配除了 \n 的所有字符

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {

            System.out.println("找到:" + matcher.group(0));

        }

    }

}
找到:h
找到:e
找到:l
找到:l
找到:o
找到: 
找到:a
找到:b
找到:c
找到: 
找到:1
找到:1
找到:1