Reputation: 28248
I need to loop through all the matches in say the following string:
<a href='/Product/Show/{ProductRowID}'>{ProductName}</a>
I am looking to capture the values in the {} including them, so I want {ProductRowID} and {ProductName}
Here is my code so far:
Dim r As Regex = New Regex("{\w*}", RegexOptions.IgnoreCase)
Dim m As Match = r.Match("<a href='/Product/Show/{ProductRowID}'>{ProductName}</a>")
Is my RegEx pattern correct? How do I loop through the matched values? I feel like this should be super easy but I have been stumped on this this morning!
Upvotes: 0
Views: 7628
Reputation: 2821
Change your RegEx pattern to \{\w*\}
then it will match as you expect.
You can test it with an online .net RegEx tester.
Upvotes: 1
Reputation: 10124
You can just group your matches using a regex like the following:
<a href='/Product/Show/(.+)'\>(.+)</a>
In this way you have $1 and $2 matching the values you want to get.
You an also give your matches names so that they aren't anonymous/position oriented for retrieval:
<a href='/Product/Show/(?<rowid>.+)'\>(?<name>.+)</a>
Upvotes: 2
Reputation: 338118
Your Pattern is missing a small detail:
\{\w*?\}
Curly braces must be escaped, and you want the non-greedy star, or your first (and only) match will be this: "{ProductRowID}'>{ProductName}"
.
Dim r As Regex = New Regex("\{\w*?\}")
Dim input As String = "<a href='/Product/Show/{ProductRowID}'>{ProductName}</a>"
Dim mc As MatchCollection = Regex.Matches(input, r)
For Each m As Match In mc
MsgBox.Show(m.ToString())
Next m
RegexOptions.IgnoreCase
is not needed, because this particular regex is not case sensitive anyway.
Upvotes: 3