Regular expressions 用于模式匹配。
要在 Excel 中使用,请按以下步骤操作:
第 1 步:将 VBA 引用添加到“Microsoft VBScript 正则表达式 5.5”
- 选择“开发者”标签 (I don't have this tab what do I do?)
- 从“代码”功能区部分选择“Visual Basic”图标
- 在“Microsoft Visual Basic for Applications”窗口的顶部菜单中选择“工具”。
- 选择“参考”
- 选中“Microsoft VBScript 正则表达式 5.5”旁边的框以包含在您的工作簿中。
- 点击“确定”
第 2 步:定义您的模式
基本定义:
- 范围。
- 例如
a-z 匹配从 a 到 z 的小写字母
- 例如
0-5 匹配从 0 到 5 的任意数字
[] 恰好匹配这些括号内的对象之一。
- 例如
[a] 匹配字母 a
- 例如
[abc] 匹配单个字母,可以是 a、b 或 c
- 例如
[a-z] 匹配字母表中的任何单个小写字母。
() 为返回目的对不同的匹配进行分组。请参阅下面的示例。
{} 之前定义的模式的重复副本的乘数。
- 例如
[a]{2} 匹配两个连续的小写字母 a:aa
- 例如
[a]{1,3} 匹配至少一个到三个小写字母 a、aa、aaa
+至少匹配一个或多个在它之前定义的模式。
- 例如
a+ 将匹配连续的 a 的 a、aa、aaa 等等
? 匹配零个或一个之前定义的模式。
- 例如模式可能存在也可能不存在,但只能匹配一次。
- 例如
[a-z]? 匹配空字符串或任何单个小写字母。
*匹配零个或多个在它之前定义的模式。
- 例如可能存在或不存在的模式的通配符。
- 例如
[a-z]* 匹配空字符串或小写字母字符串。
. 匹配除换行符\n以外的任何字符
- 例如
a. 匹配以 a 开头并以除 \n 之外的任何内容结尾的两个字符串
|OR 运算符
- 例如
a|b 表示a 或b 可以匹配。
- 例如
red|white|orange 与其中一种颜色完全匹配。
^NOT 运算符
- 例如
[^0-9] 字符不能包含数字
- 例如
[^aA] 字符不能为小写a 或大写A
\ 转义后面的特殊字符(覆盖上述行为)
锚定模式:
^ 匹配必须出现在字符串的开头
- 例如
^a 第一个字符必须是小写字母 a
- 例如
^[0-9] 第一个字符必须是数字。
$匹配必须出现在字符串的末尾
优先级表:
Order Name Representation
1 Parentheses ( )
2 Multipliers ? + * {m,n} {m, n}?
3 Sequence & Anchors abc ^ $
4 Alternation |
预定义的字符缩写:
abr same as meaning
\d [0-9] Any single digit
\D [^0-9] Any single character that's not a digit
\w [a-zA-Z0-9_] Any word character
\W [^a-zA-Z0-9_] Any non-word character
\s [ \r\t\n\f] Any space character
\S [^ \r\t\n\f] Any non-space character
\n [\n] New line
示例 1:作为宏运行
以下示例宏查看单元格A1 中的值,以查看前1 个或2 个字符是否为数字。如果是这样,它们将被删除并显示字符串的其余部分。如果没有,则会出现一个框,告诉您未找到匹配项。单元格A1 的值12abc 将返回abc,1abc 的值将返回abc,abc123 的值将返回“不匹配”,因为数字不在字符串的开头。
Private Sub simpleRegex()
Dim strPattern As String: strPattern = "^[0-9]{1,2}"
Dim strReplace As String: strReplace = ""
Dim regEx As New RegExp
Dim strInput As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("A1")
If strPattern <> "" Then
strInput = Myrange.Value
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
MsgBox (regEx.Replace(strInput, strReplace))
Else
MsgBox ("Not matched")
End If
End If
End Sub
示例 2:作为内嵌函数运行
此示例与示例 1 相同,但设置为作为单元内函数运行。要使用,请将代码更改为:
Function simpleCellRegex(Myrange As Range) As String
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String
Dim strOutput As String
strPattern = "^[0-9]{1,3}"
If strPattern <> "" Then
strInput = Myrange.Value
strReplace = ""
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.test(strInput) Then
simpleCellRegex = regEx.Replace(strInput, strReplace)
Else
simpleCellRegex = "Not matched"
End If
End If
End Function
将您的字符串 ("12abc") 放在单元格 A1 中。在单元格B1 中输入此公式=simpleCellRegex(A1),结果将为“abc”。
示例 3:循环范围
此示例与示例 1 相同,但循环遍历一系列单元格。
Private Sub simpleRegex()
Dim strPattern As String: strPattern = "^[0-9]{1,2}"
Dim strReplace As String: strReplace = ""
Dim regEx As New RegExp
Dim strInput As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("A1:A5")
For Each cell In Myrange
If strPattern <> "" Then
strInput = cell.Value
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
MsgBox (regEx.Replace(strInput, strReplace))
Else
MsgBox ("Not matched")
End If
End If
Next
End Sub
示例 4:拆分不同的模式
此示例循环遍历一个范围(A1、A2 和A3)并查找以三个数字开头、后跟一个字母字符、然后是 4 个数字的字符串。输出使用() 将模式匹配拆分为相邻的单元格。 $1 表示在第一组() 中匹配的第一个模式。
Private Sub splitUpRegexPattern()
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("A1:A3")
For Each C In Myrange
strPattern = "(^[0-9]{3})([a-zA-Z])([0-9]{4})"
If strPattern <> "" Then
strInput = C.Value
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.test(strInput) Then
C.Offset(0, 1) = regEx.Replace(strInput, "$1")
C.Offset(0, 2) = regEx.Replace(strInput, "$2")
C.Offset(0, 3) = regEx.Replace(strInput, "$3")
Else
C.Offset(0, 1) = "(Not matched)"
End If
End If
Next
End Sub
结果:
其他模式示例
String Regex Pattern Explanation
a1aaa [a-zA-Z][0-9][a-zA-Z]{3} Single alpha, single digit, three alpha characters
a1aaa [a-zA-Z]?[0-9][a-zA-Z]{3} May or may not have preceding alpha character
a1aaa [a-zA-Z][0-9][a-zA-Z]{0,3} Single alpha, single digit, 0 to 3 alpha characters
a1aaa [a-zA-Z][0-9][a-zA-Z]* Single alpha, single digit, followed by any number of alpha characters
</i8> \<\/[a-zA-Z][0-9]\> Exact non-word character except any single alpha followed by any single digit