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

来自姬鸿昌的知识库
跳到导航 跳到搜索
第139行: 第139行:
 
找到:.
 
找到:.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
 +
 +
=== 示例:在"hello abc 11.1"中搜索"[?]" ===
 +
<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 11.1";
 +
 +
        String regStr = "[?]"; //匹配 . 本身
 +
 +
        Pattern pattern = Pattern.compile(regStr);
 +
 +
        Matcher matcher = pattern.matcher(content);
 +
 +
        while (matcher.find()) {
 +
 +
            System.out.println("找到:" + matcher.group(0));
 +
 +
        }
 +
 +
    }
 +
 +
}
 +
</syntaxhighlight>控制台什么都不会输出

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

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


示例:[.] 中的 .

示例1:在"hello abc 111"中搜索"[.]"

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 = "[.]"; //匹配 . 本身

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {

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

        }

    }

}

控制台什么都不输出


示例2:示例1:在"hello abc 11.1"中搜索"[.]"

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

public class TestTemp {

    public static void main(String[] args) {

        String content = "hello abc 11.1";

        String regStr = "[.]"; //匹配 . 本身

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {

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

        }

    }

}
找到:.



示例3:相当于在"hello abc 11.1"中搜索"\\."

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

public class TestTemp {

    public static void main(String[] args) {

        String content = "hello abc 11.1";

        String regStr = "\\."; //匹配 . 本身

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {

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

        }

    }

}
找到:.


示例:在"hello abc 11.1"中搜索"[?]"

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

public class TestTemp {

    public static void main(String[] args) {

        String content = "hello abc 11.1";

        String regStr = "[?]"; //匹配 . 本身

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {

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

        }

    }

}

控制台什么都不会输出