Re: find word(s) in string

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

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 06/29/04


Date: Tue, 29 Jun 2004 13:36:37 -0500

Brian,
I would consider using a regular expression.

Something like:
        Const input As String = "One Two ICE Three"
        Const input2 As String = "One NC Two Three"
        Const input3 As String = "NC is the way"
        Const input4 As String = "The train is an ICE"

        Dim re As New
System.Text.RegularExpressions.Regex("(^|\s+)(ICE|NC)($|\s+)",
RegularExpressions.RegexOptions.Compiled)

        Debug.WriteLine(re.IsMatch(input), input)
        Debug.WriteLine(re.IsMatch(input2), input2)
        Debug.WriteLine(re.IsMatch(input3), input3)
        Debug.WriteLine(re.IsMatch(input4), input4)

If you need to know if you matched either ICE or NC, you can use a named
group, something like:

        Dim re As New
System.Text.RegularExpressions.Regex("(^|\s+)(?'tag'ICE|NC)($|\s+)",
RegularExpressions.RegexOptions.Compiled)

        Matchedwhat(re, input)
        Matchedwhat(re, input2)
        Matchedwhat(re, input3)
        Matchedwhat(re, input4)

    Private Shared Sub MatchedWhat(ByVal re As
System.Text.RegularExpressions.Regex, ByVal input As String)
        Dim m As System.Text.RegularExpressions.Match = re.Match(input)
        If m.Success Then
            Debug.WriteLine(m.Groups("tag"), "matched")
        Else
            Debug.WriteLine(False, "failed")
        End If
    End Sub

FWIW: The regular expression I gave is:

(^|\s+) = match either the beginning of the line or one or more whitespace
characters
(ICE|NC) = match either ICE or NC
($|\s+) = match either the ending of the line or one or more whitespace
characters.

Hope this helps
Jay

"BrianDH" <BrianDH@discussions.microsoft.com> wrote in message
news:184B4117-6300-42A2-B992-2C1FDE738E93@microsoft.com...
> I am trying to find "NC" or "ICE" in a string that will vary in length
> Example:
>
> String = "One Two ICE Three"
> Or
> String = "One NC Two Three"
>
> I need to know if "ICE" or "NC" is within the text string.
>
> I have tried "InStr" but it shows true if there is a word that hasthe
letters "NC" withina word.
>
> Any Help
>
> Thank you
>



Relevant Pages

  • Re: String trim (was JavaScript Functions)
    ... By "work" I assume you mean that it corrects the fact that some javascript engines do not match '\u00A0' when \s is used in a regular expression. ... But is this an attempt to create methods that conform to the ECMA specification, and thus are consistent across platforms, or is it a desire to arbitrarily include the non-breaking space in the set of matched characters for a trim function. ... The ECMA spec wants \s to match javascript's whit space characters and it line terminator characters, but the definition of whitespace includes all of Unicode's Zs group, and JScript, for example, does not match the majority of those. ... var LineTerminator = [ ...
    (comp.lang.javascript)
  • Re: Empty input box
    ... That regular expression will match a string consisting only of one or more 's' characters. ... it will return null (which type-converts to boolean false). ... That will match a string consisting of one or more whitespace characters only. ...
    (comp.lang.javascript)
  • Re: Regular Expression Function
    ... I want a regular expression to compare sentences and then rate them as ... I have an array with a list of other phrases like so... ... characters will throw things off. ... "In an hour the system will go down for maintenance". ...
    (alt.php)
  • Re: Regular Expression Function
    ... I want a regular expression to compare sentences and then rate them as ... I have an array with a list of other phrases like so.. ... These will be stripped from the input first. ... characters will throw things off. ...
    (alt.php)
  • Re: Expert script (.bat) writers help needed (strip double-quote from string)
    ... Sets or returns the regular expression pattern being searched for. ... Always a RegExp object variable. ... May include any of the regular expression characters defined in the table in the Settings section. ...
    (microsoft.public.windowsxp.help_and_support)