Link to home
Start Free TrialLog in
Avatar of siono1
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!
ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of siono1
siono1

ASKER

Hi VBRocks, thanks for your solution. I see now I didn't put all the information in my question.

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!
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.RegularExpressions

Public Class frmRegEx

    Private rx As Regex
    Private rxOpt As RegexOptions
    Private strPattern As String

    Private Sub checkboxMultiline_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkboxMultiline.CheckedChanged

        If Me.checkboxMultiline.Checked = 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!
Avatar of siono1

ASKER

That helps a lot! Thanks!