replace, using regular expressions

Jim Ault JimAultWins at yahoo.com
Mon Sep 12 18:40:50 EDT 2005


This should work for you and allow several variations and tests in the
future.
Paste code into a stack script and type ³runTest² in the msg box
 
on runtest
    put "the first test [rnNF2] the second test [rnNF3] and the third test
[rnNW3] are all here" into testIn
  answer testIn
   
  --put replaceText(testIn, "\[rnN([FWS])([1-8])\]", "[$2$1]") into testOut
  --answer testOut
  put matchFCN(testin) into testOut

  put matchText(testIn,"\[rnN([FWS])([1-8])\]", theSeason, theGrade) into
gotPhone
  answer "gotPhone=" & gotPhone & ", theSeason=" & theSeason & ", theGrade="
& theGrade
end runtest

function matchFCN tempVer
  put 1 into ch1
  put 0 into cnt
  repeat until ch1 = ""
    get matchchunk(tempVer,"\[rnN([FWS])([1-8])\]",ch1,ch2,ch3,ch4)
    if ch2 = ch1 then
      --first hit is a single char
      --now swap chars
      get char ch1 of tempVer
      put char ch3 of tempVer into char ch1 of tempVer
      put it into char ch3 of tempVer
      add 1 to cnt
    else
      --we got more than one char ??
    end if
  end repeat
  subtract 1 from cnt
  if tempVer <> testIn then
    answer cnt & " changes were made"
    answer tempVer
  else
    answer "No changes were made"
  end if
  return tempVer
end matchFCN

Jim Ault
Las Vegas



On 9/12/05 6:41 AM, "peter lemay" <plemay at readnaturally.com> wrote:

> Hi.
> 
>  
> 
> I want to do several replaces using a regular expression in Revolution.
> 
>  
> 
> Here are two runable examples, but they both have problems.
> 
> In these examples, I want these replacements:
> 
> [rnNF2] replaced by [2F]
> 
> [rnNF3] replaced by [3F]
> 
> [rnNW3] replaced by [3W]
> 
>  
> 
> The first example uses replaceText and works as a 'replace all'. But the
> 3rd parameter is a literal. When I try this, the $1 and $2 do not
> represent the (parenthesis-delimited) groups the way normal regex does.
> 
>  
> 
> The second example uses matchText and only replaces the first instance.
> I could probably use this in a repeat, but there should be a better way.
> 
>  
> 
> Any suggestions?
> 
>  
> 
> Pete
> 
>  
> 
> ***
> 
> put "the first test [rnNF2] the second test [rnNF3] and the third test
> [rnNW3] are all here" into testIn
> 
> answer testIn
> 
>  
> 
> put replaceText(testIn, "\[rnN([FWS])([1-8])\]", "[$2$1]") into testOut
> 
> answer testOut
> 
> ***
> 
> put matchText(testIn,"\[rnN([FWS])([1-8])\]", theSeason, theGrade) into
> gotPhone
> 
>  
> 
> answer "gotPhone=" & gotPhone & ", theSeason=" & theSeason & ",
> theGrade=" & theGrade
> 
> ***
> 
>  
> 
>  
> 
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list