如何在 VB.net 中编写此正则表达式? [英] How to write this regular expression in VB.net?

查看:28
本文介绍了如何在 VB.net 中编写此正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事需要我为他的 vb.net 应用编写一个正则表达式.

My coworker needs me to write him a regular expression for his vb.net app.

我不会 vb,他也不会正则表达式.

I do not know vb and he does not know regex.

他需要的正则表达式是:

The regex he needs is:

/.*web id: ?(\d+).*/i

基本上,他需要在字符串中搜索诸如web id: 345"或web id:2534"之类的内容并检索 ID.

Basically he needs to search a string for something like "web id: 345" or "web id:2534" and retrieve the ID.

他拿走了我上面给他的东西,然后把它们组合在一起:

He took what I gave him above and was able to put this together:

Dim strPattern As String = ".*web id: ?(\d+).*"
Dim strReplacement$ = "$1"

GetWebId$ = Regex.Replace(LCase$(strNote$), strPattern$, strReplacement$)

但是我不确定你是如何传递不区分大小写的标志的?(他目前对此的解决方法是首先将整个字符串小写)

However I am not sure how you pass the case-insensitive flag? (his current fix for that is making the whole string lowercase first)

还有一件事我似乎无法弄清楚是当他在多行的字符串上运行它时,任何与web id:\ d"不在同一行的文本也会被返回,我发现奇怪.

Also one thing I can't seem to figure out is when he runs this on a string with multiple lines, any text that is not on the same line as "web id: \d" is also being returned which i find strange.

推荐答案

使用 RegexOptions.IgnoreCase 标志:

Regex.Replace(strNote, strPattern, strReplacement, RegexOptions.IgnoreCase)

如果您要忽略大小写,则无需使用 LCase.我还觉得奇怪的是,您的变量名称中包含所有这些 $ 符号 - 它们在 C# 或 VB.NET 中都不应该有效.<小时>编辑 #2: 我意识到您可能想要替换与 $1 替换模式匹配的整行以匹配 ID.如果您需要使用多个选项,您可以按如下方式将它们一起使用:

If you are going to ignore case there should be no need to use LCase. I also find it odd that you have all those $ symbols in your variable names - they shouldn't be valid in either C# or VB.NET.


EDIT #2: I realize you may have wanted to replace the entire line that matched with the $1 replacement pattern to match the ID. If you have a need to use multiple options you can Or them together as follows:

Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase Or RegexOptions.Singleline)

<小时>

EDIT #1:您使用了错误的方法来提取 ID.您有一个组 (\d+) 来捕获 ID,但是您在匹配中使用了 Regex.Replace,这就是您在文本中获取其他所有内容的原因.要匹配 ID,请使用以下内容:


EDIT #1: you are using the wrong method to extract the ID. You have a group (\d+) to capture the ID, but you are using Regex.Replace on your match, which is why you get everything else in the text. To match the ID use the following:

Dim input As String = "foo web id:2010 bar"
Dim pattern As String = ".*web id: ?(\d+).*"
Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)

If m.Success Then
    Dim id As String = m.Groups(1).Value
    Console.WriteLine("ID: " & id)
Else
    Console.WriteLine("No Match!")
End If

您会注意到我们引用了 Groups(1),它保存了由 (\d+) 组捕获的值.具有更多组的模式可能会导致混淆,尤其是嵌套组.在这些情况下,您可以使用命名组.以下是更新为使用命名组的相同代码:

You will notice we refer to Groups(1) which holds the value captured by the (\d+) group. Patterns with more groups may lead to confusion, especially with nested groups. In those cases you can use named groups. Here is the same code updated to use named groups:

Dim input As String = "foo web id:2010 bar"
Dim pattern As String = ".*web id: ?(?<ID>\d+).*" ' group name added '
Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)

If m.Success Then
    ' refer to group by group name '
    Dim id As String = m.Groups("ID").Value
    Console.WriteLine("ID: " & id)
Else
    Console.WriteLine("No Match!")
End If

这篇关于如何在 VB.net 中编写此正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆