gpt4 book ai didi

regex - 使用 VB.net Regex 提取字符串的两个部分

转载 作者:行者123 更新时间:2023-12-02 06:33:50 28 4
gpt4 key购买 nike

我的字符串遵循格式 "x Packs of y" 所以例如 "15 packs of 5", "1 pack of 10" 等等。

我想使用一个正则表达式来查找 "x Packs of y" 并将 x 放在一个变量中,将 y 放在一个变量中第二个变量。

有人可以告诉我该怎么做吗?

最佳答案

试试这个...

Dim foo = "15 packs of 5"

Dim match = Regex.Match(foo, "(\d+) packs? of (\d+)", RegexOptions.IgnoreCase)

Dim x = match.Groups(1).Value
Dim y = match.Groups(2).Value

Console.WriteLine("x = " & x)
Console.WriteLine("y = " & y)

Live Demo - Fiddle

更新:感谢 Braj 指出包/包。

关于regex - 使用 VB.net Regex 提取字符串的两个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24834335/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com