tips
[Top] [All Lists]

REALbasic Tip: Finding whole words Part II

To: "REALbasic Tips" <realbasic-tips at lists dot realsoftware dot com>
Subject: REALbasic Tip: Finding whole words Part II
From: Geoff Perlman <geoff at realsoftware dot com>
Date: Tue, 7 Oct 2003 10:36:22 -0500
Last week's tip was a simple function using Regular Expressions to find whole words in a string. The function was intentionally kept simple to introduce Regular Expressions but it does have a flaw. Searching for some special characters (like the ones used in Regular Expressions) would not work. Here's an updated function that is quite a bit more complex in its implementation but provides a more robust whole word search. The good news is you can use this function just as you did the last one without understanding how it's implemented.

Function FindWholeWord(source as string, find as string) As integer
   dim i as integer
   dim findCleaned, char as string
   dim re as regEx
   dim match as regExMatch

   'make sure that our string is recognized as a literal (ie. doesn't
   'contain any regex characters)
   for i = 1 to Len(find)
      char = mid(find, i, 1)
      if (LenB(char) = 1) then
         'convert all of our one byte chars to hex
         findCleaned = findCleaned + "\x" + Hex(Asc(char))
      else
         'we will leave any multi byte characters as is
         findCleaned = findCleaned + char
      end if
   next

   re = new regEx

   re.searchPattern = "(?<!\w)" + findCleaned + "(?!\w)"
   match = re.search(source)

   if (match <> nil) then
      'return the one-based string position
      return match.SubExpressionStartB(0) + 1
   else
      // didn't find anything
      return 0
   end if
End Function

This function was provided by Cortis Clark.
--
Geoff Perlman
President and CEO
REAL Software, Inc.
512-328-7325 x711 (voice)
512-328-7372 (fax)


- - - - - - - - - -
Got a useful tip to share? Send it to us at:
<REALbasic-tips at lists dot realsoftware dot com>

Click here to unsubscribe:
<http://support.realsoftware.com/listmanager/>

<Prev in Thread] Current Thread [Next in Thread>
  • REALbasic Tip: Finding whole words Part II, Geoff Perlman <=