1

I have a dataset that contains multiple values. I want to take those rows from that dataset from the datatable BLABLA that contains an "S" with the numbers from zero to six. Then I want to display those in a MessageBox.

My Regex is S[0-6].

Dim answer As String = ""
Dim myregex As Regex = New Regex("S[0-6]")

Dim SearchRows() As DataRow = datasetB.Tables("BLABLA").Select("Data LIKE  '%myregex%'")

For k As Integer = 0 To SearchRows.Length - 1
    If answer = "" Then
        answer = SearchRows(k).Item("Data")
    Else
        answer = answer & vbNewLine & SearchRows(k).Item("Data")
    End If
Next

MsgBox(answer)

Unfortunately SearchRows is empty. I couldn't find the reason by debugging. What am I doing wrong?

1 Answers1

1

The DataTable.Select method does not support regex. As the documentation states, it does allow you to pass it a filterExpression string as an argument, but just because it takes a filter expression doesn't mean that it support's regex expressions. On the contrary, it's designed to mostly support the same kinds of expressions as the WHERE clause in T-SQL. T-SQL's LIKE operator does not support regex patterns, and neither does DataTable.Select. See this documentation to learn the rules for the pattern expressions that are supported by the DataTable.Select method's LIKE operator.

The filter expressions supported by the LIKE operator are not as advanced as regex, so it's almost certainly impossible to construct a filter expression which is that specific. If there is a way to filter to digits between 0 and 6, I am unaware of it and the documentation doesn't mention it. So, if you really need to filter rows by regex, you can still do it, but you need to select all the rows and then filter them yourself:

Dim SearchRows() As DataRow = datasetB.Tables("BLABLA").Select().
    Where(Function(r) myregex.IsMatch(r.Item("Data").ToString())).
    ToArray()
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • How can I display the whole Row in the MessageBox? Till now it is only the column named `Data`. –  Aug 06 '19 at 13:09
  • I'm not sure how to answer that because I'm not sure what part of it you don't understand. It seems like you already asked that and had it answered [here](https://stackoverflow.com/a/57205385/1359668). So if that doesn't answer your question already, I'd need more clarification regarding what you really need and what exactly you don't understand. Regardless, it's a different question, best asked as a new question rather than addressed in the comments here :) – Steven Doggart Aug 06 '19 at 13:13