Regex to search contain string


I want to remove some rows in a listbox using regex.
the rows contains the regex pattern has to be removed

Listbox.addrow "«ab1"
Listbox.addrow "000"
Listbox.addrow "«ab0"
Listbox.addrow "L000"

  dim row as integer
    for row=0 to Listbox1.ListCount -1
    dim rx1 as new RegEx
    rx1.SearchPattern = "^.*\b(000|ab1|ab0|L)\b.*$"
    dim match1 as RegExMatch = rx1.Search((Listbox1.Cell(row,0)))
    if match1 is nil then
      Listbox1.RemoveRow row
    end if

but this one is not working. any help ?


Try this:

Var oRegEx As New RegEx
Var oMatch As RegExMatch

oRegEx.SearchPattern = ".*\b(000|ab1|ab0|L)\b.*"

For iRow As Integer = Listbox1.LastRowIndex DownTo 0
  oMatch = oRegEx.Search(Listbox1.CellValueAt(iRow, 0))
  If Not (oMatch Is Nil) Then
  End If

By the way, your code upstairs works. From your description it’s hard to see what exactly the result should be after the loop in the Listbox.


I am forgot to mention, I am still using xojo 2014 r4.3, because i need it to run on windows xp system.

yes its run on xojo 2019, but not in 2014.


Btw your code should go from the last row down to 0 …

And the L in the pattern seems unnecessary if you already check for 000 …

Martin’s code is better, but when removing rows, you have to work backwards or you’ll end up skipping rows.

Also, don’t overmatch. You only care about those particular patterns, so there is no need to match on the whole line.


You might actually be better off using InStr instead of RegEx …

Try writing that code and you’ll change your mind. :grinning:

Well, if it is just those 4 and not variations (eg L\d\d\d ) then InStr is simpler.

As it is the pattern is not clear either - does he really want to remove all rows with L in them?

If he only wants to remove rows with L000 then the L is redundant anyway.

RegEx is great, but you have to put more time into learning it.

In his case InStr might have accomplished the same better and faster. It depends on the scenario.

And funnily enough Kem, your pattern won’t find L000 if I’m not mistaken … :wink:

Actually, it might not find three of the four patterns … (not at my computer to check)

using this method is works.

but how to avoid the number with leading zero from removing

actually, the rows inserted are,

Listbox.addrow "«ab1"
Listbox.addrow "000"
Listbox.addrow "046"
Listbox.addrow "«ab0"
Listbox.addrow "L000"

i want to remove everything but keep only the row with number and leading zero, so the “046” should stay.


Is the leading zero at the beginning of a row or anywhere in the text?



Bla 046 bla

Pattern is row: “^0\d*$”
Pattern is at beginning of row: “^0\d*”
Pattern is in row: “0\d*” (but that allows L0056xat)
Pattern is word in row: “\b0\d*\b”

^ beginning of line
0 Followed by a zero
\d* Followed by numbers
$ End of line
\b word boundary

For i = ListBox1.Ubound DownTo 0
If Not Match then
LB.removeRow row
End if
Next i

You’re right, I should have looked harder at the pattern rather than copy-and-pasting it. In my defense, I was focussed on the major problem we both saw of working the rows backwards.

I agree that InStr (or IndexOf) will be better in many cases, but not this one. This pattern is not “contains one of these strings”, but “contains one of these words”, a big difference, and one that is hard to emulate.

For example, you said

but this pattern wouldn’t match that whereas a simple InStr would.

To clarify Markus’ advice, if the list of the what you want to keep is shorter than the what you want to remove, match on that and remove whatever doesn’t match.

1 Like


its worked now :slightly_smiling_face:


1 Like