Re: Compare a Variable Against Multiple Values

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: post messages (post.messages_at_gmail.com)
Date: 02/23/05


Date: 23 Feb 2005 10:29:49 -0800

I have been trying to figure this out too. The regex you have for
testing individual words is what I came up with too. Unfortunately, it
also matches partial strings like catdog, dogcat, catty. Any tricks to
forcing "whole word" logic?

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message news:<O5hTmawFFHA.3756@TK2MSFTNGP10.phx.gbl>...
> Jason,
> In addition to the other comments:
> > If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
> Consider using OrElse instead of Or, as Or will cause all the equations to
> be evaluated before returning a result, OrElse will return the result after
> the first match. For example, if myVar = "A":
>
> > If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
> Will compare myVar, to "A", then to "B", the to "C" before it returns True.
>
> Where as:
>
> > If myVar = "A" OrElse myVar = "B" OrElse myVar = "C" Then...
> Will compare myVar to "A", and return True! (checking for "B" or "C" is not
> needed as "A" is True!)
>
> ---x--- begin earlier post of mine ---x---
> I would use the RegEx as its IMHO the "simplest" & "cleanest"
> implementation! However! you need to understand RegEx to be comfortable
> using it, luckily this regex is easy. Also you may need to get over the
> perception that RegEx is slow. Yes it has some inherit overhead the other
> methods do not, however that overhead may be warranted for the simplicity of
> the routine. Also based on the context the overhead of the RegEx may be
> lower, significantly lower, then other routines.
>
> Note: I would only use the RegEx method for checking Strings, if I was
> creating an "In" for other types I would pick an algorithm that was more
> friendly for that type.
>
> Here's one possibility for an "In" function based on RegEx.
>
> Public Shared Function [In](ByVal input As String, ByVal ParamArray
> words() As String) As Boolean
> Dim pattern As String = String.Join("|", words)
> Return System.Text.RegularExpressions.Regex.IsMatch(input, pattern)
> End Function
>
> Then to use it you simple need to:
>
> strTest = "The cat jumped over the sleepy dog."
> If [In](strTest, "cat","dog", "sleep") Then
> Debug.WriteLine("Found strings")
> Else
> Debug.WriteLine("Did NOT find strings!")
> End If
>
> If the above RegEx version proved to be a performance problem I would then
> consider using a For Each loop.
>
> Public Shared Function [In](ByVal input As String, ByVal ParamArray
> words() As String) As Boolean
> For Each word As String In words
> If input.IndexOf(word) <> -1 Then
> Return True
> End If
> Next
> Return False
> End Function
>
> I would consider creating an object that contained a list of words, which
> had a method the checked to see if a string had one of those words.
>
> Public Class ValidValues
>
> Private ReadOnly m_regex As System.Text.RegularExpressions.Regex
>
> Public Sub New(ByVal ParamArray words() As String)
> Dim pattern As String = String.Join("|", words)
> m_regex = New System.Text.RegularExpressions.Regex(pattern,
> System.Text.RegularExpressions.RegexOptions.Compiled)
> End Sub
>
> Public Function IsMatch(ByVal value As String) As Boolean
> Return m_regex.IsMatch(value)
> End Function
>
> End Class
>
>
> Dim values As New ValidValues("cat", "dog", "sleep")
> If values.IsMatch(strTest) Then
> Debug.WriteLine("Found strings")
> Else
> Debug.WriteLine("Did NOT find strings!")
> End If
> Return
>
> Dim statusValues As New ValidValues("ACTIVE","OTHER")
> If statusValues.IsMatch(Status) Then
> ...
>
> Notice in both cases that the actual method (algorithm) of matching is
> hidden (encapsulated) within either the object or the function itself. Which
> allows you to replace the Algorithm with a more efficient one if needed...
>
> Whether I used the Function or Class would depend on how the function or
> class was being within my program... Using Refactoring
> http://www.refactoring.com I can change between the two...
>
> As the others pointed out there are other equally valid ways to implement
> the above. There are also methods that have yet to be mentioned, such as
> using a HashTable or using a DataSet, plus still others. Which one you
> should use REALLY depends on the context of what you are doing!
>
> For example if your status value is in a DataTable, you can use a filter
> statement that include an "In" statement, on either DataTable.Select or
> DataView.RowFilter. Like wise of the list of valid statues are in a
> DataTable, you could use DataTable.Rows.Find assuming that the status was
> the primary key to that table, or use filter & either DataTable.Select or
> DataView.RowFilter...
>
> Hope this helps
> Jay
>
> ---x--- end earlier post of mine ---x---
>
> Another example routine:
>
> Public Shared Function IsInSet(ByVal item As Object, ByVal ParamArray
> setItems() As Object) As Boolean
> Return (Array.IndexOf(SetItems, Item) >= 0)
> End Function
>
>
> Hope this helps
> Jay
>
> "Jason" <someone@microsoft.com> wrote in message
> news:eg$Wb3eFFHA.3728@TK2MSFTNGP14.phx.gbl...
> > Is there a mechanism in VB.NET that allows something like:
> >
> > If myVar In ("A","B","C") Then...
> >
> > The way I'm doing it now is:
> >
> > Select Case myVar
> > Case "A","B","C"
> >
> > Or like this:
> >
> > If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
> >
> > I'm just wondering if there's some handy new syntax in .NET to handle
> > this.
> >



Relevant Pages

  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex below you end up with an empty regex: ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness in front of it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: Regex hangs
    ... > I have a regex for matching VB6-functions, but it hangs on one ... > Workstation As String, Optional IsReadOnly As Boolean = False, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex Capture problem
    ... "learned" my regex using a freeware utility that had slightly different ... was trying to capture instead of. ... I have used Regex utilities before, so I understand the concepts of text ... Function RESub(str As String, SrchFor As String, ReplWith As String) As String ...
    (microsoft.public.excel.programming)