vba-正则表达式2(常用符号)

'正则表达式的核心是设置对比的规则,也就是设置Pattern属性,而组成这些规则除了字符本身以外,是具有特定含义的符号。
 '下面介绍的是正规表达式中常用符号的第一部分。
 
'\号

  '1.放在不便书写的字符前面,如换行符(\r),回车符(\n),制表符(\t),\自身(\\)
 
  '2.放在有特殊意义字符的前面,表示它自身,"\$","\^","\."
 
  '3.放在可以匹配多个字符的前面
      
       '\d 0~9的数字
       '\w 任意一个字母或数字或下划线,也就是 A~Z,a~z,0~9,_ 中任意一个
       '\s 包括空格、制表符、换页符等空白字符的其中任意一个
       
       '以上改为大写时,为相反的意思,如\D 表示非数字类型
       
        Sub t1()
           Dim regx As New RegExp
           Dim sr
           sr = "AE45B646C"
           With regx
             .Global = True
             .Pattern = "\d" '排除非数字
             Debug.Print .Replace(sr, "")
           End With
        End Sub
'.(点)

   '可以匹配除换行符以外的所有字符

  也是一个点位符 \.

'+号
   '+表示一个字符可以有任意多个重复的。但至少有一个
    
   Sub t11()
     Dim regx As New RegExp
     Dim sr
     sr = "A234CA7A"
     With regx
      .Global = True
      .Pattern = "A\d+"
      Debug.Print .Replace(sr, "")
     End With
     
   End Sub
'{}号
  '可以设置重复次数
    '1 {n} 重复n次
        Sub t16()
           Dim regx As New RegExp
           Dim sr
           sr = "A234CA7A67"
           With regx
            .Global = True
            .Pattern = "\d{5}" '连续5个数字
            Debug.Print .Replace(sr, "")
           End With
           
         End Sub
   '2  {m,n}最小重复m次,最多重复n次
     
        Sub t22()
           Dim regx As New RegExp
           Dim sr
           sr = "A234CA7A6789"
           With regx
            .Global = True
            .Pattern = "\d{4,5}" '连续两个数字或连续三个数字
            Debug.Print .Replace(sr, "")
           End With
         End Sub
    '3 {m,} 最少重复m次,相当于+
         Sub t23()
           Dim regx As New RegExp
           Dim sr
           sr = "A2348t6CA7A67"
           With regx
            .Global = True
            .Pattern = "\d{2,}" '连续两个数字或连续三个数字
            Debug.Print .Replace(sr, "")
           End With
         End Sub
         
'* 可以出现0等任意次   相当于 {0,},比如:"\^*b"可以匹配 "b","^^^b"...

' ?
  '1 匹配表达式0次或者1次,相当于 {0,1},比如:"a[cd]?"可以匹配 "a","ac","ad"

        Sub t24()
           Dim regx As New RegExp
           Dim sr
           sr = "A23.48CA7A6..7"
           With regx
            .Global = True
            .Pattern = "\d+\.?\d+" '最多连续1个
            Debug.Print .Replace(sr, "")
           End With
         End Sub
    '2 利用+?的格式可以分段匹配
          
      Sub t87()
        Dim regex As New RegExp
        Dim sr, mat, m
        sr = "<td><p>aa</p></td> <td><p>bb</p></td>"
        With regex
          .Global = True
          .Pattern = "<td>.*?</td>"
         Set mat = .Execute(sr)
          For Each m In mat
            Debug.Print m
          Next m
        End With
      End Sub

 

     Sub t88()
              
        Dim regex As New RegExp
        Dim sr, mat, m
        sr = " aba  aca  ada "
        With regex
          .Global = True
          .Pattern = "\s.+?\s"
         Set mat = .Execute(sr)
          For Each m In mat
            Debug.Print m
          Next m
        End With

     End Sub

posted on 2021-05-23 16:32  松梅  阅读(2390)  评论(0编辑  收藏  举报

导航