Regular Expression

cwalsh1979

New member
Joined
Mar 15, 2007
Messages
2
Programming Experience
1-3
Does anyone know how to retrieve the each of the values in the following list and put them into an array? With VB.NET and regular expressions....

HTML:
<ucms:menuitem>
<li><a href="<ucms:menuitem_url>"><ucms:menuitem_name></a></li>
</ucms:menuitem>
 
<ucms:menuitem>
<li class="subnav"><a href="<ucms:menuitem_url>"><ucms:menuitem_name></a></li>
</ucms:menuitem>
 
<ucms:menuitem>
<li class="subnav2"><a href="<ucms:menuitem_url>"><ucms:menuitem_name></a></li>
</ucms:menuitem>
The resulting array should look like this:

array("<li><a href="<ucms:menuitem_url>"><ucms:menuitem_name></a></li>",
"<li class="subnav"><a href="<ucms:menuitem_url>"><ucms:menuitem_name></a></li>",
"<li class="subnav2"><a href="<ucms:menuitem_url>"><ucms:menuitem_name></a></li>")
 
Last edited by a moderator:
Although it look like Xml (and I would use Xml methods if it was) this regex captures the three sections:
VB.NET:
Dim pattern As String = "<ucms:menuitem>.+?</ucms:menuitem>"
Dim sc As New Specialized.StringCollection
For Each m As Match In Regex.Matches(input, pattern, RegexOptions.Singleline)
    sc.Add(m.Value)
Next
Match and Regex classes are from System.Text.RegularExpressions namespace so you can import this or qualify those uses in code. Input variable used is the String containing that markup code you posted. I just added the match strings to a StringCollection, easier to work with than the strict array.
 
Thanks JohnH, this will definitely work for me. I would have used the DOM or XML object, but they would only work correctly if the surrounding HTML was correctly formed and I cannot guarantee this.

Cheers
 
Back
Top