VB.Net 正则表达式 - 提取通配符值

发布于 2024-11-24 08:24:47 字数 302 浏览 0 评论 0原文

我需要帮助从正则表达式匹配中提取通配符的值。例如:

正则表达式:“我喜欢*”

输入:“我喜欢巧克力”

我希望能够从正则表达式匹配中提取字符串“巧克力”(或不管还有什么)。如果可能的话,我还希望能够从单个通配符匹配中检索多个通配符值。例如:

正则表达式:“我演奏*和*”

输入:“我演奏吉他和贝斯”

我希望能够提取两个“吉他”和“低音”。有办法做到吗?

I need help extracting the value of a wildcard from a Regular Expressions match. For example:

Regex: "I like *"

Input: "I like chocolate"

I would like to be able to extract the string "chocolate" from the Regex match (or whatever else is there). If possible, I also want to be able to retrieve several wildcard values from a single wildcard match. For example:

Regex: "I play the * and the *"

Input: "I play the guitar and the bass"

I want to be able to extract both "guitar" and "bass". Is there a way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我为君王 2024-12-01 08:24:47

一般来说,正则表达式利用组的概念。组由括号表示。

所以我喜欢
我会喜欢 (.
) 吗? = 所有字符 * 表示前面有多个字符或没有前面的字符

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+) 和 (\w+) :这将匹配 I Like then a space 和一个或多个单词字符,然后是 and 一个空格和 一个或多个单词字符

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

要更完整地处理正则表达式,请查看在这个网站上有一个很棒的教程。

In general regex utilize the concepts of groups. Groups are indicated by parenthesis.

So I like
Would be I like (.
) . = All character * meaning as many or none of the preceding character

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

The above code will work for and string that has I Like and will print out all characters after including the ' ' as . matches even white space.

Your second case is more interesting because the first rx will match the entire end of the string you need something more restrictive.

I Like (\w+) and (\w+) : this will match I Like then a space and one or more word characters and then an and 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

For a more complete treatment of regex take a look at this site which has a great tutorial.

你的笑 2024-12-01 08:24:47

这是我在 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

Here is my RegexExtract Function in VBA. It will return just the sub match you specify (only the stuff in parenthesis). So in your case, you'd write:

 =RegexExtract(A1, "I like (.*)")

Here is the code.

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

Here is a version that will allow you to use multiple groups to extract multiple parts at once:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文