|
楼主 |
发表于 2012-10-4 08:37
|
显示全部楼层
- '6) Test 方法
- ' 返回一个布尔值,该值指示正则表达式是否与字符串成功匹配。
- Sub r_6()
- Dim regEX As Object
- Dim x As String, y As String
- Dim i As Integer
- x = "a1b2c3"
- Set regEX = CreateObject("VBScript.RegExp")
- With regEX
- .Global = True
- .Pattern = "\d"
- For i = 1 To Len(x)
- If .Test(Mid(x, i, 1)) Then y = y & Mid(x, i, 1)
- Next i
- End With
- MsgBox y 'y返回123
- End Sub
复制代码- Public Sub ty2() '提取英文字母并去重复
- Dim arr, i&, y, str$
- str = "$A$8,$D$11,$D$5,,$D$10,$G$12"
- With CreateObject("vbscript.regexp")
- .Global = True
- .Pattern = "[$,0-9]"
- str = .Replace(str, "")
- .Pattern = "(.)\1{1,}"
- str = .Replace(str, "$1")
- End With
- Debug.Print str
- End Sub
复制代码- '把文件中的空白行去掉:假设d盘有一个文本text.txt,内容如下
- '
- '8 5 4 1 6 3 2 7 9
- '
- '7 6 2 9 5 8 3 4 1
- '
- '9 3 1 4 2 7 8 5 6
- '
- '
- '
- '6 9 3 8 7 5 1 2 4
- '
- '5 1 8 3 4 2 6 9 7
- '
- '2 4 7 6 1 9 5 3 8
- '
- '
- '
- '3 26 7 8 4 9 1 5
- '
- '4 8 9 5 3 1 7 6 2
- '
- '1 7 5 2 9 6 4 8 3
- '
- '正则表达式:\r\n\r\n
- '
- '分析:\r\n匹配一个回车+换行组合,windows操作系统中把它作为文本行的结束标签。
- '使用正则表达式\r\n\r\n进行的搜索将匹配两个连续的行尾标签,而这正好是空白行。
- '注意:Unix和Linux操作系统中只使用一个换行符来结束一个文本行,换句话说,
- '在Unix或Linux系统中匹配空白行只使用\n\n即可,不需要加上\r
- Sub test8()
- f = "d:\Text.txt"
- s = CreateObject("scripting.FileSystemObject").OpenTextFile(f, 1, 1, -2).ReadAll
- With CreateObject("VBSCRIPT.REGEXP")
- .Global = True
- .Pattern = "(\r\n){2}"
- CreateObject("scripting.FileSystemObject").OpenTextFile(f, 2, 1).Write .Replace(s, "")
- End With
- End Sub
复制代码- '例1、有如下一组电话号码,如何改变成右侧的样式。
- ':假设d盘有一个文本text.txt,内容如下
- '(020)12345678 020-12345678
- '(021)32145678 021-32145678
- '(0371)45678129 0371-45678129
- '(0392)1234567 0392-1234567
- '(010)21458965 010-21458965
- '(0393)45987636 0393-45987636
- '(0372)87654321 0372-87654321
- Sub ff()
- f = "d:\Text.txt"
- s = CreateObject("scripting.FileSystemObject").OpenTextFile(f, 1, 1, -2).ReadAll
- With CreateObject("VBSCRIPT.REGEXP")
- .Global = True
- .Pattern = "\((\d{3,4})\)(\d+)" '设置正则表达式
- CreateObject("scripting.FileSystemObject").OpenTextFile(f, 2, 1).Write .Replace(s, "$1-$2")
- End With
- End Sub
- '正则表达式的三步曲应该是:1、查找;2、引用匹配了的文本(后向引用);3、有选择地替换文本。
- '案例:
- '我们的电话格式通常都是:(区号)电话,比如说:(029)8401132;现在假设我们要求把文本中所有的电话格式都改为:029-8401132,我们可以这样做:
- 'Text
- '(020)82514769
- '(021)83281314
- '(029)88401132
- 'regEX
- '\((\d{3})\)(\d{8})
- '注:这段其实也可以写成\(\d{3}\)\d{8},之所以给\d{3}和\d{8}分别用括号加起来,是因为替换的时候需要引用括号里面的内容。
- 'Replace
- '$1-$2
- '注:大部分语言的正则表达式实现,在查找中,使用后向引用来代表一个子模式,其语法是“\数字”;而在替换中,其语法是“$数字”。
- '所以$1就表示引用刚才匹配到的第一部分,$2是匹配到的第二部分,中间用连字符-。
- 'Result
- '020-82514769
- '021-83281314
- '029-88401132
复制代码- '提取括号里的百分数的数字(即匹配结果为0.39)
- Sub tes2()
- Dim str As String
- str = "M4.1 點缺陷*7(0.39%)"
- With CreateObject("vbscript.regexp")
- .Global = True
- .Pattern = ".+\((\d+\.?\d*)%\)"
- MsgBox .Replace(str, "$1") '符合模式的为:(0.39%),替换后=0.39
- End With
- End Sub
复制代码- '取出末尾两个数字 30 CAC0040 取出40
- Sub tes3()
- Dim str As String
- str = "30 CAC0040 取出40"
- With CreateObject("vbscript.regexp")
- .Global = True
- .Pattern = "\d+$"
- MsgBox .Execute(str)(0)
- End With
- End Sub
复制代码- '统一空格个数
- Public Sub tongyi()
- Dim str
- str = "script type = text/javascript"
- With CreateObject("vbscript.regexp")
- .Global = True
- .Pattern = "\s+"
- MsgBox .Replace(str, " ")
- End With
- End Sub
复制代码- '判断字符串是不是由数字组成
- Public Sub pd()
- Dim str, ExTest
- str = "123456"
- With CreateObject("vbscript.regexp")
- .Global = True '设置全局可用
- .Pattern = "^\d*$" '设置样式
- ExTest = .Test(str) '执行搜索测试
- MsgBox ExTest
- End With
- End Sub
复制代码- [/code]
- [code]'老朽的作品 0-4为小,5-9为大
- '将 "1237865409" 转换成 "小小小大大大大小小大"
- Sub zldcc()
- a = "1237865409"
- Set regEX = CreateObject("VBSCRIPT.REGEXP") 'RegEx为建立正则表达式
- regEX.Global = True '设置全局可用
- regEX.Pattern = "[0-4]" '样式"
- a = regEX.Replace(a, "小")
- regEX.Pattern = "[5-9]" '样式"
- MsgBox regEX.Replace(a, "大")
- End Sub
复制代码- 'firstindex属性,怎么理解
- Sub match()
- Dim reg As Object
- Dim s As String
- Dim i As Byte
- Dim match As Object, matches As Object
- Set reg = CreateObject("vbscript.regexp")
- s = "a11b22c32d43e"
- i = 1
- With reg
- .Global = True
- .Pattern = "\d+"
- Set matches = .Execute(s)
- End With
- For Each match In matches
- MsgBox "matches集合中第" & i & "个元素在字符串中" & vbCrLf _
- & "位置:" & match.FirstIndex & vbCrLf _
- & "长度为:" & match.Length & vbCrLf _
- & "值为:" & match.Value
- i = i + 1
- Next
- End Sub
复制代码- '这个能否取出字符串“右8-4、7-3、6-2、6-1“中两个数字加一个-:8-4 等
- Function tiqu(rng As Range, n As Integer)
- Set regx = CreateObject("VBscript.regexp")
- With regx
- .Global = True
- .Pattern = "\d+-\d+"
- Set matchs = .Execute(rng)
- tiqu = matchs.Item(n)
- End With
- End Function
复制代码 |
|