“正则表达式 元字符 转义符号”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
第32行: 第32行:
 
     public static void main(String[] args) {
 
     public static void main(String[] args) {
  
         String content = "abc$)";
+
         String content = "abc$(";
  
 
         String regStr = "$";
 
         String regStr = "$";

2022年11月15日 (二) 09:49的版本

https://www.bilibili.com/video/BV1Eq4y1E79W/?p=6

基本介绍

如果要灵活地运用正则表达式,必须了解其中各种元字符的功能,元字符从功能上大致为:

  • 限定符
  • 选择匹配符
  • 分组组合和反向引用符
  • 特殊字符
  • 字符匹配符
  • 定位符




元字符(metacharactor)——转义号 \\

\\ 符号说明:在使用正则表达式去检索某些特殊字符的时候,需要用到转义符号,否则检索不到结果,甚至会报错。

案例:

用 $ 去匹配 “abc$(”会怎样?

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

/**
 * 演示转义字符的使用
 */
public class RegExp02 {

    public static void main(String[] args) {

        String content = "abc$(";

        String regStr = "$";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {

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

        }

    }
}
找到:

用 ( 去匹配 “abc$(”会怎样?