vb.net 正则表达式获取两个字符串之间的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19866464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to get text between two strings
提问by KelvZhan
I want to get dynamic sixseven-digit numbers as shown below:
我想获得动态六位七位数字,如下所示:
id="tid_3660328">
and append them to the end of TextBox1.
并将它们附加到 TextBox1 的末尾。
In other words, I want to get the number: 3660328
换句话说,我想得到这个数字:3660328
From between: id="tid_
从中间: id="tid_
and: ">
和:>
My question is how I could do this in VB.NET. My first thought was "regex", which is a topic I have zero experience on. I appreciate the help.
我的问题是如何在 VB.NET 中做到这一点。我的第一个想法是“正则表达式”,这是我零经验的主题。我很感激你的帮助。
Note: I was thinking I could use the code here but with my own regex: https://stackoverflow.com/a/9332731
注意:我想我可以在这里使用代码,但使用我自己的正则表达式:https: //stackoverflow.com/a/9332731
回答by Steven Doggart
This is a good place for using RegEx.
这是使用 RegEx 的好地方。
If you only want to find numbers that are exactly seven digits you could use this RegEx pattern:
如果您只想查找恰好为七位数字的数字,则可以使用此 RegEx 模式:
id="tid_(\d{7})">
Or, if you don't care how many digits it is, you could use this pattern:
或者,如果您不在乎它是多少位数,则可以使用以下模式:
id="tid_(\d+)">
Here's what the pattern means:
这是模式的含义:
id="tid_- Matching strings must begin with this text(...)- Creates a group so that we can later access the value of just this part of the match.\d- Any numeric digit character{7}- Seven numeric characters in a row">- Matching strings must end with this text
id="tid_- 匹配的字符串必须以此文本开头(...)- 创建一个组,以便我们稍后可以访问匹配的这一部分的值。\d- 任何数字字符{7}- 连续七个数字字符">- 匹配的字符串必须以此文本结尾
In the second pattern, the +, which replaces the {7}just means one-or-more instead of exactly seven.
在第二种模式中,+替换了{7}just 的 表示一个或多个,而不是正好七个。
In VB.NET, you can search an input string using a RegEx pattern, like this:
在 VB.NET 中,您可以使用 RegEx 模式搜索输入字符串,如下所示:
Public Function FindNumbers(input As String) As List(Of String)
Dim numbers As New List(Of String)()
Dim pattern As String = "id=""tid_(\d{7})"">"
For Each i As Match In Regex.Matches(input, pattern)
numbers.Add(i.Groups(1).Value)
Next
Return numbers
End Function
Notice that in the string literal in VB, you have to escape the quotation marks by doubling them. You'll also notice that, instead of using i.Value, we are using i.Groups(1).Value. The reason is that i.Valuewill equal the entire matched string (e.g. id="tid_3660328">), whereas group 1 will equal just the number part (e.g. 3660328).
请注意,在 VB 中的字符串文字中,您必须通过将引号加倍来对引号进行转义。您还会注意到i.Value,我们使用的是 ,而不是使用i.Groups(1).Value。原因是i.Value它将等于整个匹配的字符串(例如id="tid_3660328">),而组 1 将等于数字部分(例如3660328)。
Update
更新
To answer your question below, to call this function and output it to a TextBox, you could do something like this:
要在下面回答您的问题,调用此函数并将其输出到 a TextBox,您可以执行以下操作:
Dim numbers As List(Of String) = FindNumbers("id=""tid_3660328"">")
Text1.Text = String.Join(Environment.NewLine, numbers.ToArray())
回答by gpmurthy
Consider the following Regex...
考虑以下正则表达式...
(?<=tid_).*?(?=\"\>)
Explanation:
解释:
- (?<=tid_) : Match the Prefix tid_ but exclude it from the capture
- .*? : Any Character, any number of repetitions, as few as possible
- (?=\">) : Match the suffix "> but exclude it from the capture
- (?<=tid_) :匹配前缀 tid_ 但将其从捕获中排除
- .*? : 任意字符,任意数量的重复,尽可能少
- (?=\">) :匹配后缀 "> 但将其从捕获中排除

