Search and Replace

Ken Ray kray at sonsothunder.com
Tue Oct 15 21:14:01 EDT 2002


> On Wednesday, Oct 16, 2002, at 08:41 Australia/Sydney, Dan Friedman
> wrote:
>
> > Greetings!
> >
> > I need to switch the gender of some text.  But, I'm having a hard time
> > figuring out a way to do it.  Obviously I can't do a simple replace
> > because
> >
> > Replace "HE" with "SHE" in someText
> >
> > Would turn "HELP" into "SHELP"
> >
> > I've written this function about a dozen times and it still isn't
> > working
> > perfectly.
>
> I feel a regex coming on. Are you there, Ken?


I'm here!  :-) It's a little tricky (what with the capitalization and
everything), but here goes:

function replaceGender whatText
  local tStart,tEnd
  put "he,him,himself,his,he's,boy,guy" into SearchWordsMale
  put "she,her,herself,her,she's,girl,gal" into SearchWordsFemale
  set the wholeMatches to true
  repeat forever
    get
matchChunk(whatText,"[^A-Za-z0-9](boy|h(e|im|is|imself|e's)|guy)[^A-Za-z0-9]
",tStart,tEnd)
    if it is true then
      put char tStart to tEnd of tText into tFoundChunk
      put (charToNum(toUpper(char 1 of tFoundChunk)) = charToNum(char 1 of
tFoundChunk) into isUpper
      put itemOffset(tFoundChunk,SearchWordsMale) into tItem
      if tItem <> 0 then
        put item tItem of SearchWordsFemale into temp
        if isUpper then put toUpper(char 1 of temp) into char 1 of temp
        put temp into char tStart to tEnd of whatText
      else
        -- you decide... bail? error out? ignore?
      end if
    else
      exit repeat
    end if
  end repeat
  return whatText
end replaceGender

Have fun!

Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/




More information about the use-livecode mailing list