Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS RegEx Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /RegEx/PCRE2/PCRE2 Test
This example is the version from Wed, 26th Apr 2022.
Project "PCRE2 Test.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Function UnhandledException(error As RuntimeException) As Boolean
If error IsA PCRE2ExceptionMBS Then
MessageBox error.Message
return true
End If
End EventHandler
End Class
Class Window1 Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control SearchPatternField Inherits TextField
ControlInstance SearchPatternField Inherits TextField
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control ReplacePattern Inherits TextField
ControlInstance ReplacePattern Inherits TextField
EventHandler Sub TextChange()
LastStartPos = -1
End EventHandler
End Control
Control TextArea1 Inherits TextArea
ControlInstance TextArea1 Inherits TextArea
End Control
Control SearchButton Inherits PushButton
ControlInstance SearchButton Inherits PushButton
EventHandler Sub Action()
Search
End EventHandler
End Control
Control ReplaceButton Inherits PushButton
ControlInstance ReplaceButton Inherits PushButton
EventHandler Sub Action()
Substitute False
End EventHandler
End Control
Control ReplaceAllButton Inherits PushButton
ControlInstance ReplaceAllButton Inherits PushButton
EventHandler Sub Action()
Substitute true
End EventHandler
End Control
Control CheckCaseless Inherits CheckBox
ControlInstance CheckCaseless Inherits CheckBox
EventHandler Sub Action()
compiler.Caseless = Me.Value
End EventHandler
End Control
Control CheckWholeWord Inherits CheckBox
ControlInstance CheckWholeWord Inherits CheckBox
EventHandler Sub Action()
compiler.MatchWord = Me.Value
End EventHandler
End Control
EventHandler Sub Open()
compiler = New PCRE2CompilerMBS
compiler.Caseless = CheckCaseless.Value
compiler.Multiline = True
compiler.NewLine = compiler.kNewLineAny
compiler.MatchWord = CheckWholeWord.Value
TextArea1.SelectionStart = 0
End EventHandler
Sub Search()
compiler.Pattern = SearchPatternField.Text
Dim code As PCRE2CodeMBS = compiler.Compile
Dim match As New PCRE2MatchDataMBS(code)
Dim StartPos As Integer = TextArea1.SelectionStart + TextArea1.SelectionLength
If StartPos < 0 Then
StartPos = 0 // no selection
End If
// avoid endless staying at same position
If StartPos = LastStartPos Then
StartPos = StartPos + 1
End If
LastStartPos = StartPos
Dim n As Integer = code.Match(TextArea1.Text, match, StartPos)
If n > 0 Then
// found
Dim o0 As Integer = match.OffsetVector(0)
Dim o1 As Integer = match.OffsetVector(1)
TextArea1.SelectionStart = match.StartPosition
TextArea1.SelectionLength = o1-o0
System.DebugLog "Found at "+match.StartPosition.ToText
Else
// move back to start
TextArea1.SelectionLength = 0
TextArea1.SelectionStart = 0
System.DebugLog "not found"
Beep
End If
End Sub
Sub Substitute(all as Boolean)
compiler.Pattern = SearchPatternField.Text
Dim code As PCRE2CodeMBS = compiler.Compile
code.SubstituteGlobal = all
Dim match As New PCRE2MatchDataMBS(code)
// preserve selection
Dim SelectionStart As Integer = TextArea1.SelectionStart
Dim SelectionLength As Integer = TextArea1.SelectionLength
Dim StartPosition As Integer
If all Then
StartPosition = 0
Else
StartPosition = TextArea1.SelectionStart
End If
TextArea1.Text = code.Substitute(TextArea1.Text, ReplacePattern.Text, match, StartPosition)
// restore selection
TextArea1.SelectionStart = SelectionStart
TextArea1.SelectionLength = SelectionLength
End Sub
Property LastStartPos As integer
Property compiler As PCRE2CompilerMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project
See also:
The items on this page are in the following plugins: MBS RegEx Plugin.