Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Dynamically set regexoptions at runtime (VB.NET)

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

840 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