tags:

views:

2067

answers:

3

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!

+1  A: 

Change your RegEx pattern to \{\w*\} then it will match as you expect.

You can test it with an online .net RegEx tester.

RickL
Site referenced is down.
mmcglynn
Ok, here's another one:http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
RickL
+2  A: 

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.

Tomalak
+1  A: 

Hey,

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>
David in Dakota