RegexMBS newline problem

RegexMBS and me don’t see eye to eye. Again I wanted to speed up a Xojo regex by using RegexMBS. But RegexMBS doesn’t find anything. The commented out Xojo regex works fine:

[code]dim theFolderitem as FolderItem = SpecialFolder.Desktop.Child(“test.emlx”)
if theFolderitem = Nil or not theFolderitem.Exists then Return

dim theBinStream as BinaryStream = BinaryStream.Open(theFolderitem)
dim MessageText as String = theBinStream.Read(10 * 1024)

'dim theRegex as new RegEx
'theRegex.searchPattern = “(
|\r)Message-ID: ?(.*)(
|\r)”
'dim theRegexMatch as RegExMatch = theRegex.search(MessageText)
'if theRegexMatch = nil then
'return “”
'elseif theRegExMatch.SubExpressionCount >= 2 then
'return theRegexMatch.subExpressionString(2).Trim
'end if

MessageText = MessageText.DefineEncoding(Encodings.UTF8)
dim theRegexMBS as new RegExMBS
theRegexMBS.CompileOptionUngreedy = true
theRegexMBS.CompileOptionMultiline = True
theRegexMBS.CompileOptionNewLineAny = True
dim result as string
if theRegexMBS.Compile("(
|\r)Message-ID: ?(.*)(
|\r)") then
if theRegexMBS.Match(MessageText) then
result = theRegexMBS.Substring(2)
else
result = “nichts gefunden”
end if
end if[/code]

I uploaded the test file to https://www.mothsoftware.com/downloads/test.zip. It can only be a newline problem. But the explanations for CompileOptionNewLineAny are confusing.

By the way, looking at the variables in the debugger makes the debugger crash. I had a Messagebox theRegexMBS.Substring(2) for testing. That also makes the debugger crash. Latest MBS, Xojo 2019r2.1. High Sierra.

I would use this pattern to use ^ and $ for line start and end: “^Message-ID: ?(.*)$”

Also add caseless option:
theRegexMBS.CompileOptionCaseLess = True

In my test Email it’s Message-Id with a small case d.
Next I fixed Substring(), which won’t work with Match function. You need to use Execute:

So finally this works:

Dim theRegexMBS As New RegExMBS theRegexMBS.CompileOptionUngreedy = True theRegexMBS.CompileOptionMultiline = True theRegexMBS.CompileOptionNewLineAnyCRLF = True theRegexMBS.CompileOptionCaseLess = True Dim result As String If theRegexMBS.Compile("(\ |\\r)Message-ID: ?(.*)(\ |\\r)") Then If theRegexMBS.Execute(MessageText) > 0 Then result = theRegexMBS.Substring(2) Else result = "nichts gefunden" End If End If MsgBox result

Thanks!