siono1
asked on
Dynamically set regexoptions at runtime (VB.NET)
Hi,
I have am using VB.NET and have a form with checkboxes for each of the possible regexoptions (MultiLine, SingleLine, IgnoreCase, etc.). However I do not know how to dynamically set the options on my regex at runtime according to whether the checkbox is checked or not. Has anyone any idea how to do this? Any help would be greatly appreciated.
Thanks!
I have am using VB.NET and have a form with checkboxes for each of the possible regexoptions (MultiLine, SingleLine, IgnoreCase, etc.). However I do not know how to dynamically set the options on my regex at runtime according to whether the checkbox is checked or not. Has anyone any idea how to do this? Any help would be greatly appreciated.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here you go - use this. If the user checks the checkbox, it will add it to the class variable, if they uncheck the checkbox, then it will remove it from the class variable.
Imports System.Text.RegularExpress ions
Public Class frmRegEx
Private rx As Regex
Private rxOpt As RegexOptions
Private strPattern As String
Private Sub checkboxMultiline_CheckedC hanged(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles checkboxMultiline.CheckedC hanged
If Me.checkboxMultiline.Check ed = True Then
rxOpt = rxOpt Or RegexOptions.Multiline
rx = New Regex(strPattern, rxOpt)
Else
rxOpt = CType(rxOpt - RegexOptions.Multiline, RegexOptions)
rx = New Regex(strPattern, rxOpt)
End If
End Sub
End Class
Hope that helps!
Imports System.Text.RegularExpress
Public Class frmRegEx
Private rx As Regex
Private rxOpt As RegexOptions
Private strPattern As String
Private Sub checkboxMultiline_CheckedC
If Me.checkboxMultiline.Check
rxOpt = rxOpt Or RegexOptions.Multiline
rx = New Regex(strPattern, rxOpt)
Else
rxOpt = CType(rxOpt - RegexOptions.Multiline, RegexOptions)
rx = New Regex(strPattern, rxOpt)
End If
End Sub
End Class
Hope that helps!
ASKER
That helps a lot! Thanks!
ASKER
Your solution is almost perfect but what if the "rxOptDefault = RegexOptions.IgnoreCase OR RegexOptions.Multiline OR RegexOptions.Singleline" and I check the checkBoxCompiled and checkBoxIgnoreWhite and uncheck checkBoxSingleline. How can I combine these new options with the new default options without having to write lots of if-then statements? You can see how it would easily get complicated!
Thanks again!