Help with convert to MBS RegEX

I’ve read that the MBS RegEX plugin is a bit faster than Xojo’s built in one, and I have a need for a performance boost. I have a very simple loop that iterates the XML finding instances of %generate_id% and replacing each one with a unique identifier.

I’ve looked at the three or four included examples but I’m not seeing enough information to make this transition easy enough to want to do. Perhaps I’m missing something.

Is there anyone experienced enough with RegExMBS to help me convert this very simple Xojo code?

// Replace IDs needing to be generated
dim trxReplaceID as new RegEx
trxReplaceID.SearchPattern = "(\%generate_id\%)"

// Sequentially number them because why not
while trxReplaceID.Search(msOutput, 0) <> nil
  trxReplaceID.ReplacementPattern = GetGeneratedID
  msOutput = trxReplaceID.Replace(msOutput, 0)
  
wend

Here is a (very) simple example:

var t as string = "123"

var rx as new RegExMBS
if rx.Compile( "\d" ) then
  var start as integer
  while rx.Execute( t, start ) > 0
    t = rx.Replace( "a" )
    start = rx.Offset( 1 )
  wend
end if

But this replaces one character with another so the string lengths are the same. Your code should compare the length of the string you’re replacing with the length of its replacement and adjust start accordingly.

As an alternative, have you considered something like this?

var arr() as string = msOutput.Split( "%generate_id%" )
if arr.Count > 1 then
  var newArr() as string
  for index as integer = 0 to arr.LastRowIndex - 1
    newArr.AddRow arr( index )
    newArr.AddRow GetGeneratedId
  next
  newArr.AddRow arr( arr.LastRowIndex )
  msOutput = String.FromArray( newArr, "" )
end if

(Untested.)

Edits to code as I go.

Thanks for the help, Kem! The string split / join method blows the RegEx methods out of the water.

For science, this is what I came up with for MBS RegEx, though I guessed on how the Offset thing worked.

// Replace IDs
dim toRX as new RegExMBS
if toRX.Compile("(\%generate_id\%)") then
  dim tiReplaceCursor as Integer
  while toRX.Execute(msOutput, tiReplaceCursor) > 0
    dim tsReplaceID as String = GetGeneratedID
    msOutput = toRX.Replace(tsReplaceID)
    
    tiReplaceCursor = toRX.Offset(tsReplaceID.Len)
    
  wend
  
end
1 Like

I’ll bet it was so fast you had to wonder if it did anything at all. :slight_smile:

It really was that different! I’d designed this whole process from the start with the idea of replacing the IDs as a final step. It’s funny how I hadn’t even thought about string split and replace, I’d just gone straight for the RegEx. I really appreciate your help! Thank you again :slight_smile:

1 Like