Platforms to show: All Mac Windows Linux Cross-Platform
/Tools/RegEx/RegExMBS
Function:
Required plugins for this example: MBS Tools Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Tools/RegEx/RegExMBS
This example is the version from Mon, 18th Mar 2012.
Function:
Required plugins for this example: MBS Tools Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Tools/RegEx/RegExMBS
This example is the version from Mon, 18th Mar 2012.
Project "RegExMBS.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
Class Window1 Inherits Window
Control EditField1 Inherits TextField
ControlInstance EditField1 Inherits TextField
End Control
Control EditField2 Inherits TextArea
ControlInstance EditField2 Inherits TextArea
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
run
End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
runNext
End EventHandler
End Control
Control EditField3 Inherits TextField
ControlInstance EditField3 Inherits TextField
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action()
ReplaceSelection
End EventHandler
End Control
Control Separator1 Inherits Separator
ControlInstance Separator1 Inherits Separator
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action()
LoadFile
End EventHandler
End Control
Control PushButton5 Inherits PushButton
ControlInstance PushButton5 Inherits PushButton
EventHandler Sub Action()
Benchmark
End EventHandler
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control EditField4 Inherits TextField
ControlInstance EditField4 Inherits TextField
End Control
Control EditField5 Inherits TextField
ControlInstance EditField5 Inherits TextField
End Control
Control EditField6 Inherits TextField
ControlInstance EditField6 Inherits TextField
End Control
Control EditField7 Inherits TextField
ControlInstance EditField7 Inherits TextField
End Control
Control EditField8 Inherits TextField
ControlInstance EditField8 Inherits TextField
End Control
Control EditField9 Inherits TextField
ControlInstance EditField9 Inherits TextField
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control StaticText4 Inherits Label
ControlInstance StaticText4 Inherits Label
End Control
Control StaticText5 Inherits Label
ControlInstance StaticText5 Inherits Label
End Control
Control StaticText6 Inherits Label
ControlInstance StaticText6 Inherits Label
End Control
Control StaticText7 Inherits Label
ControlInstance StaticText7 Inherits Label
End Control
Control StaticText8 Inherits Label
ControlInstance StaticText8 Inherits Label
End Control
Control StaticText9 Inherits Label
ControlInstance StaticText9 Inherits Label
End Control
Control StaticText10 Inherits Label
ControlInstance StaticText10 Inherits Label
End Control
Control StaticText11 Inherits Label
ControlInstance StaticText11 Inherits Label
End Control
EventHandler Sub Open()
LoadFile SpecialFolder.Desktop.Child("test.xml") // just take some text file to test
// you may use someother UTF8 file.
End EventHandler
Protected Sub Benchmark()
RunBenchmarkRB EditField4.text,EditField5.text,StaticText4,"RB1.txt"
RunBenchmarkRB EditField6.text,EditField7.text,StaticText5,"RB2.txt"
RunBenchmarkRB EditField8.text,EditField9.text,StaticText6,"RB3.txt"
RunBenchmarkMBS EditField4.text,EditField5.text,StaticText8 ,"MBS1.txt"
RunBenchmarkMBS EditField6.text,EditField7.text,StaticText9 ,"MBS2.txt"
RunBenchmarkMBS EditField8.text,EditField9.text,StaticText10,"MBS3.txt"
End Sub
Protected Sub LoadFile()
dim f as FolderItem
f=GetOpenFolderItem("text")
if f<>Nil then
LoadFile f
end if
End Sub
Protected Sub LoadFile(f as folderitem)
dim b as BinaryStream
b=f.OpenAsBinaryFile(false)
if b<>Nil then
if b<>nil then
TextFile=b.Read(b.Length,Encodings.UTF8)
end if
if TextFile="" then
MsgBox "Failed to read file content from "+f.Name+"."
end if
end if
End Sub
Protected Sub ReplaceSelection()
dim s as string
dim t as string
s=EditField3.text
s=ConvertEncoding(s,Encodings.UTF8) // lib requires UTF8 strings
// ask for selection replace string
t=last.ReplaceSelection(s)
// correct result offset so future searches will be correct
ResultOffset=ResultOffset+len(t)-len(EditField2.SelText)
EditField2.SelText=t
End Sub
Protected Sub RunBenchmarkMBS(pattern as string, replacetext as string, output as Label,filename as string)
dim r as RegExMBS
dim s as string
dim t as integer
dim b as BinaryStream
dim f as FolderItem
t=ticks
r=new RegExMBS
r.CompileOptionMultiline=True
r.ExecuteOptionNoUTF8Check=true
r.CompileOptionNoUTF8Check=true
r.CompileOptionUngreedy=true
call r.Compile pattern
call r.Study
s=r.ReplaceAll(TextFile, replacetext)
t=ticks-t
if s="" then
output.Text="error! "+Format(t/60,"0.0")+" s"
else
output.text=Format(t/60,"0.0")+" s"
end if
f=SpecialFolder.Desktop.Child(filename)
b=f.CreateBinaryFile("text")
if b<>Nil then
b.Write s
b.Close
end if
End Sub
Protected Sub RunBenchmarkRB(pattern as string, replacetext as string, output as Label, filename as string)
dim r as RegEx
dim s as string
dim t as integer
dim b as BinaryStream
dim f as FolderItem
t=ticks
r=new RegEx
r.ReplacementPattern=replacetext
r.SearchPattern=pattern
r.Options.ReplaceAllMatches=true
r.Options.Greedy=false
s=r.Replace(TextFile)
t=ticks-t
if s="" then
output.Text="error!"
else
output.text=Format(t/60,"0.0")+" s"
end if
f=SpecialFolder.Desktop.Child(filename)
b=f.CreateBinaryFile("text")
if b<>Nil then
b.Write s
b.Close
end if
End Sub
Protected Sub run()
dim r as RegExMBS
dim i,c as integer
dim s as string
r=new RegExMBS
if not r.ConfigUTF8 then
MsgBox "UTF8 support is missing. Please complain to Christian Schmitz."
end if
r.CompileOptionUngreedy=True
r.CompileOptionMultiline=true
s=EditField1.text
s=ConvertEncoding(s,Encodings.UTF8) // lib requires UTF8 strings
if r.Compile(s) then
s=EditField2.text
s=ConvertEncoding(s,Encodings.UTF8) // lib requires UTF8 strings
s=ReplaceLineEndings(s,EndOfLine.UNIX) // lib is compiled for chr(10) as line ending. Problem maybe?
if r.Execute(s,0)>0 then
EditField2.SelStart=r.OffsetCharacters(0)
EditField2.Sellength=r.OffsetCharacters(1)-r.OffsetCharacters(0)
EditField2.SetFocus
Resultoffset=0
preLength=EditField2.Sellength
last=r
else
beep // nothing found
end if
else
MsgBox "failed to compile"
end if
End Sub
Protected Sub runNext()
dim nextChar as string
dim start as integer
if last=nil then // not yet searched
beep
else
// continue search with last text on last end position
if preLength=0 then //for "^", "$", "\b" and etc.
// get next one character
nextChar=Mid(last.text, last.OffsetCharacters(0)+1, last.OffsetCharacters(1)-last.OffsetCharacters(0)+1)
start=last.Offset(1)+LenB(nextChar)
else
start=last.Offset(1)
end if
if last.Execute(last.text,start)>0 then
EditField2.SelStart=last.OffsetCharacters(0)+ResultOffset
EditField2.Sellength=last.OffsetCharacters(1)-last.OffsetCharacters(0)
EditField2.SetFocus
else
MsgBox "Nothing found." // nothing found
end if
end if
End Sub
Property Protected Resultoffset As integer
Property Protected TextFile As String
Property Protected last As regexmBS
Property Protected preLength As integer
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
End Class
End Project
The items on this page are in the following plugins: MBS Tools Plugin.
Feedback: Report problem or ask question.