正则表达式学习

水深无声 2023-07-04 04:58 92阅读 0赞

文章目录

    • 零宽度断言
      • (?= X )
      • (?! X )
      • (?<= X )
      • (?!= X )
    • 模式修正符
      • i
      • x
      • m
      • s
    • 捕获组与非捕获组
      • 捕获组
      • 非捕获组

零宽度断言

主要包括四种:(?= X ),(?! X ),(?<= X ),(?<! X )。前两种是位于表达式右侧,后两种是位于表达式左侧。带“=”的是匹配模式,带“!”是非匹配模式。

(?= X )

  1. String testStr = "#5 abc 0#";
  2. // 含义:数字,且该数字后面紧跟#
  3. Pattern pattern = Pattern.compile("\\d(?=#)");
  4. Matcher matcher = pattern.matcher(testStr);
  5. // 0
  6. while (matcher.find()) {
  7. System.out.println(matcher.group(0));
  8. }

(?! X )

  1. String testStr = "#5 abc 0#";
  2. // 含义:数字,且该数字后面不紧跟#
  3. Pattern pattern = Pattern.compile("\\d(?!#)");
  4. Matcher matcher = pattern.matcher(testStr);
  5. // 5
  6. while (matcher.find()) {
  7. System.out.println(matcher.group(0));
  8. }

(?<= X )

  1. String testStr = "#5 abc 0#";
  2. // 含义:数字,且该数字前面紧跟#
  3. Pattern pattern = Pattern.compile("(?<=#)\\d");
  4. Matcher matcher = pattern.matcher(testStr);
  5. // 5
  6. while (matcher.find()) {
  7. System.out.println(matcher.group(0));
  8. }

(?!= X )

  1. String testStr = "#5 abc 0#";
  2. // 含义:数字,且该数字前面不紧跟#
  3. Pattern pattern = Pattern.compile("(?<!#)\\d");
  4. Matcher matcher = pattern.matcher(testStr);
  5. // 0
  6. while (matcher.find()) {
  7. System.out.println(matcher.group(0));
  8. }

模式修正符

以(?)开头加上特殊字母(i/x/m/s)表示特定模式

i

忽略大小写

  1. String testStr = "#5 Abc 0#";
  2. // 含义:忽略abc 字母大小写
  3. Pattern pattern = Pattern.compile("(?i)abc");
  4. Matcher matcher = pattern.matcher(testStr);
  5. // Abc
  6. while (matcher.find()) {
  7. System.out.println(matcher.group(0));
  8. }
  9. // 含义:不忽略abc 字母大小写
  10. pattern = Pattern.compile("(?-i)abc");
  11. matcher = pattern.matcher(testStr);
  12. // 无
  13. while (matcher.find()) {
  14. System.out.println(matcher.group(0));
  15. }
  16. // 含义:只对a忽略字母大小写
  17. pattern = Pattern.compile("(?i:a)bc");
  18. matcher = pattern.matcher(testStr);
  19. // Abc
  20. while (matcher.find()) {
  21. System.out.println(matcher.group(0));
  22. }

x

忽略空白

  1. String testStr = "#5 abc a b c 0#";
  2. // 含义:忽略a b c 中空格
  3. Pattern pattern = Pattern.compile("(?x)a b c");
  4. Matcher matcher = pattern.matcher(testStr);
  5. // abc
  6. while (matcher.find()) {
  7. System.out.println(matcher.group(0));
  8. }

m

视作多行

  1. String testStr = "#5 abc\nabc\n0#";
  2. // 含义:.不包括换行符\n
  3. Pattern pattern = Pattern.compile("(?m)abc.");
  4. Matcher matcher = pattern.matcher(testStr);
  5. //无
  6. while (matcher.find()) {
  7. System.out.println(matcher.group(0));
  8. }

s

视作单行

  1. String testStr = "#5 abc\nabc\n0#";
  2. // 含义:将换行符\n视作普通字符
  3. Pattern pattern = Pattern.compile("(?s)abc.");
  4. Matcher matcher = pattern.matcher(testStr);
  5. /* abc
  6. abc
  7. */
  8. while (matcher.find()) {
  9. System.out.println(matcher.group(0));
  10. }

捕获组与非捕获组

捕获组

以()作为捕获组的分割符,以(?:)作为非捕获组的分割符。
捕获组中:group(0)代表全部;从左至右的”(“决定捕获组的序号。

  1. String testStr = "ppsd 12#Amm 3#sa";
  2. // 含义:12#Amm
  3. Pattern pattern = Pattern.compile("((12)(#A))mm");
  4. Matcher matcher = pattern.matcher(testStr);
  5. /*
  6. 12#Amm
  7. 12#A
  8. 12
  9. #A
  10. */
  11. while (matcher.find()) {
  12. System.out.println(matcher.group(0));
  13. System.out.println(matcher.group(1));
  14. System.out.println(matcher.group(2));
  15. System.out.println(matcher.group(3));
  16. }

非捕获组

以(?:)开头,不具有捕获组序号。

  1. String testStr = "ppsd 12#Amm 3#sa";
  2. // 含义:12#Amm
  3. Pattern pattern = Pattern.compile("((?:12)(#A))mm");
  4. Matcher matcher = pattern.matcher(testStr);
  5. /*
  6. 12#Amm
  7. 12#A
  8. #A
  9. */
  10. while (matcher.find()) {
  11. System.out.println(matcher.group(0));
  12. System.out.println(matcher.group(1));
  13. System.out.println(matcher.group(2));
  14. }

发表评论

表情:
评论列表 (有 0 条评论,92人围观)

还没有评论,来说两句吧...

相关阅读

    相关 表达式学习

    < type=”text/javascript”> 众所周知,在程序开发中,难免会遇到需要匹配、查找、替换、判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决

    相关 表达式学习

    一、概述 正则表达式用于文本内容的查找和替换。 正则表达式内置于其它语言或者软件产品中,它本身不是一种语言或者软件。 [正则表达式在线工具][Link 1] 二、

    相关 表达式学习笔记

    正则表达式无论是在爬虫还是其它的应用中都是有一定作用的。 1、常见的匹配模式 模式                           描述 \w         

    相关 表达式通用学习

    本文是Jan Goyvaerts写的教程的译文,版权归原作者所有。 对一些地方进行了标注,是很好的正则表达式学习文档,通过本文档可以达到对正则表达式有一整体了解,便于以后更深