Reputation: 140
I want to get dynamic six seven-digit numbers as shown below:
id="tid_3660328">
and append them to the end of TextBox1.
In other words, I want to get the number: 3660328
From between: 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.
Note: I was thinking I could use the code here but with my own regex: https://stackoverflow.com/a/9332731
Upvotes: 2
Views: 8455
Reputation: 43743
This is a good place for using RegEx.
If you only want to find numbers that are exactly seven digits you could use this RegEx pattern:
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 textIn the second pattern, the +
, which replaces the {7}
just means one-or-more instead of exactly seven.
In VB.NET, you can search an input string using a RegEx pattern, like this:
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.Value
will equal the entire matched string (e.g. id="tid_3660328">
), whereas group 1 will equal just the number part (e.g. 3660328
).
To answer your question below, to call this function and output it to a TextBox
, you could do something like this:
Dim numbers As List(Of String) = FindNumbers("id=""tid_3660328"">")
Text1.Text = String.Join(Environment.NewLine, numbers.ToArray())
Upvotes: 4
Reputation: 2427
Consider the following Regex...
(?<=tid_).*?(?=\"\>)
Explanation:
Upvotes: 2