Solved

Dynamically set regexoptions at runtime (VB.NET)

Posted on 2007-03-20
4
321 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 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