RegExMatch.SubExpressionStartB: Character Position

I am trying to write a method to deal with determining a character position using RegEx. I am not one who uses MemoryBlocks in my code routinely. I am vaguely aware of problems that can occur when string characters are not strictly one byte. I do understand the difference between zero and one-based character position. That is not the issue. I go to the Xojo Documentation page for

RegExMatch.SubExpressionStartB (matchNumber as Integer) As Integer

It says that it " Returns the starting byte offset of the SubExpression given by matchNumber . SubExpressionStartB is zero-based."

The page has a note: To convert this byte offset into a 1-based character position (for use with String functions such as Mid and Left), use this code:

Var m As MemoryBlock
m = theString
characterPosition = m.LeftB(aRegExMatch.SubExpressionStartB(matchNumber)).Len + 1

I try and use the information in this note in my own code which is as follows

Var re As New RegEx
Var characterPosition As Integer
Var theString As String = "SomeExample"
Var aRegExMatch As RegExMatch
Var m As MemoryBlock
m = theString
re.SearchPattern = "x"
aRegExMatch = re.Search(theString) //  re.Search(m) also cause error
characterPosition = m.LeftB(aRegExMatch.SubExpressionStartB(0)).Len + 1

I get an error message at the line

characterPosition = m.LeftB(aRegExMatch.SubExpressionStartB(0)).Len + 1

Which says:

Extension method REALbasic.Len requires a conversion from class MemoryBlock to type String

I thought I was using the code that had been suggested by the documentation but I get an error. Why?

Why do you use a memoryblock at all? Use a string instead.

I was hoping that this somehow got around the problem of some characters being more than one byte and that was why a memory block was used. Why else would the note in the documentation take this circuitous route?

Haven’t a clue why the docs are so odd. I’ve never used a memoryblock for regexes. It depends on what you want to do with the character length.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.