Solved

Dynamically set regexoptions at runtime (VB.NET)

Posted on 2007-03-20
4
320 Views
Last Modified: 2008-02-26
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!
0
Comment
Question by:siono1
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 18759959
The CheckBox has a "CheckedChanged" event, you can use that to change the values as follows:

Imports System.Text.RegularExpressions

Public Class frmRegEx
    Private rx As Regex
    Private rxOptDefault As RegexOptions = RegexOptions.None
    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
            rx = New Regex(strPattern, RegexOptions.Multiline)

        Else
            rx = New Regex(strPattern, rxOptDefault)

        End If

    End Sub
End Class

Hope that helps!
0
 

Author Comment

by:siono1
ID: 18760191
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!
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18760411
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!
0
 

Author Comment

by:siono1
ID: 18762665
That helps a lot! Thanks!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now