Regex to replace text

Hi,

I want to replace text by using regex, i am using xojo 2021 r.1.1
the code seems to work but not replacing the text that start with pipe followed by the number

Listbox data

Test 1 | 1234567
Test 2 | 234567
Test 3 | 678903
dim nrow as integer
for nrow = 0 to Listbox1.listcount -1
  dim rx1 as new RegEx
  rx1.SearchPattern = "\| [0-9]+"
  dim match1 as RegExMatch = rx1.Search((Listbox1.Cell(nrow, 0)))
  if match1 is nil then
    
  else
    Listbox1.cell(nrow, 0) = rx1.Replace(Listbox1.cell(nrow, 0), "")
    
  end if
next


thanks
arief

Is it always going to be the whole of the line past the |? You could just use:

Var nPos = rx1.IndexOf( "|" )
if nPos <> -1 then rx1 = rx1.Left( nPos - 2 ) ' Minus 2 if you don't want the | or -1 if you want it.

You haven’t defined a ReplacementPattern and the documentation doesn’t show Replace as accepting a second parameter… does this code compile?

https://documentation.xojo.com/api/text/regular_expressions/regex.html#regex-replace

(Posted from iPad so I can’t be more useful)

thanks,

Yes, I missed that one, sorry.
both solution are worked

regards,
arief

Damn, i was too slow… :slight_smile:

Just for reference, an API 2 Snippet:

Var rx As New RegEx
rx.SearchPattern = "\| [0-9]+"
rx.ReplacementPattern = ""

Var rxOptions As RegExOptions = rx.Options
rxOptions.ReplaceAllMatches = True

For nrow As Integer = 0 To Listbox1.LastRowIndex
  
  Listbox1.CellTextAt(nrow, 0) = rx.Replace( Listbox1.CellTextAt(nrow, 0) )
  
Next
2 Likes