我需要帮助从正则表达式匹配中提取通配符的值。例如:
Regex: "I like *"
输入: "I like chocolate"
我希望能够从正则表达式匹配中提取字符串"巧克力"(或其他任何东西)。如果可能的话,我还希望能够从单个通配符匹配中检索多个通配符值。例如:
正则表达式:"我玩* *"
输入: "I play the guitar and the bass"
我希望能够同时提取"吉他"one_answers"低音"。有办法吗?
通常regex使用组的概念。组由括号表示。
所以我喜欢
是I like (.)。=所有字符*表示前面的字符相同或不相同
Sub Main()
Dim s As String = "I Like hats"
Dim rxstr As String = "I Like(.*)"
Dim m As Match = Regex.Match(s, rxstr)
Console.WriteLine(m.Groups(1))
End Sub
上面的代码将适用于具有I Like的字符串,并将打印出包含' ' as之后的所有字符。甚至匹配空白。
第二种情况更有趣,因为第一个rx将匹配字符串的整个末尾,您需要更严格的限制。
I Like (w+) and (w+):这将匹配I Like then a space
和一个或多个单词字符,然后匹配and
一个空格和one or more word characters
Sub Main()
Dim s2 As String = "I Like hats and dogs"
Dim rxstr2 As String = "I Like (w+) and (w+)"
Dim m As Match = Regex.Match(s2, rxstr2)
Console.WriteLine("{0} : {1}", m.Groups(1), m.Groups(2))
End Sub
关于正则表达式的更完整的处理,请查看这个网站,那里有一个很棒的教程。
这是我在VBA中的RegexExtract函数。它将只返回您指定的子匹配(只返回括号中的内容)。在你的例子中,你会写:
=RegexExtract(A1, "I like (.*)")
代码如下:
Function RegexExtract(ByVal text As String, _
ByVal extract_what As String) As String
Application.ScreenUpdating = False
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = extract_what
RE.Global = True
Set allMatches = RE.Execute(text)
RegexExtract = allMatches.Item(0).submatches.Item(0)
Application.ScreenUpdating = True
End Function
这是一个允许你使用多个组一次提取多个部分的版本:
Function RegexExtract(ByVal text As String, _
ByVal extract_what As String) As String
Application.ScreenUpdating = False
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
Dim i As Long
Dim result As String
RE.Pattern = extract_what
RE.Global = True
Set allMatches = RE.Execute(text)
For i = 0 To allMatches.Item(0).submatches.count - 1
result = result & allMatches.Item(0).submatches.Item(i)
Next
RegexExtract = result
Application.ScreenUpdating = True
End Function