0

My regex code for Visual Basic is not returning output for following code:

Dim regex As Regex = New Regex("<table class=""listing"">.*?</table>")
Dim match As Match = regex.Match(str, RegexOptions.IgnoreCase)
If match.Success Then
    MsgBox(match.Value)
End If

Input is like this:

<table class="listing">
<tr> 
<th width="85%">
Name
</th>
<th width="15%">
Day Added
</th>
</tr>
</table>
GSerg
  • 76,472
  • 17
  • 159
  • 346
Amandeep Gupta
  • 161
  • 2
  • 15

1 Answers1

0

There are multiple errors:

First, you are using it wrong: When using regexInstance.Match(), the second parameter is the start-offset - not the Regex Options.

To use Regex Options, use the Static Version of Match() along with a String Pattern:

Dim regexStr As String = "<table class=""listing"">.*?</table>"
Dim match As Match = Regex.Match(Str, regexStr, RegexOptions.IgnoreCase Or RegexOptions.Singleline)

or add the options to the constructor:

Dim regex As Regex = New Regex("<table class=""listing"">.*?</table>", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Dim match As Match = regex.Match(str)

Second, you need to Provide RegexOptions.SingleLine additional to IgnoreCase. (So: RegexOptions.IgnoreCase Or RegexOptions.SingleLine)

The HTML String contains Linebreaks, which your Regex simple doesn't match.

In Single-Line Mode the . will also match New Lines https://msdn.microsoft.com/en-us/library/yd1hzczs%28v=vs.110%29.aspx#Singleline

putting all together:

Dim Str As String = "<table class=""listing"">" & System.Environment.NewLine & "test" & System.Environment.NewLine & "</table>"

Dim regexStr As String = "<table class=""listing"">.*?</table>"
Dim match As Match = Regex.Match(Str, regexStr, RegexOptions.IgnoreCase Or RegexOptions.Singleline)

If match.Success Then
    MsgBox(match.Value)
End If
dognose
  • 20,360
  • 9
  • 61
  • 107