replacement not working properly in livecode - livecode

Iam using this code for replacing, but in some instance(begining of the line where there is no space). How to overcome this?
Shall we use regular expression (\n\|\s) in that code?
put the htmlText of field "myTextField"" into myHtml
set the caseSensitive to true
replace " re" with " sa" in myHtml
set the htmlText of fld "myTextField" to myHtml

Assuming you only want instances of "re" changed at the beginning of a word, try looping through each word, test whether if begins with "re" and change accordingly:
repeat with i = 1 to the number of words in myHtml
if char 1 to 2 of word i of myHtml = "re" then replace "re" with "sa" in word i of myHtml
end repeat
Note: the reason I'm not using 'repeat for each' is that you can't make changes to the thing being parsed in 'repeat for each'

I think the fastest solution is to do a double replace
replace " re" with " ha" in field "myTextField"
replace return & "re" with return & "ha" in field "myTextField"
There is a replaceText which lets you use regular expressions but then you can't use \1 in LiveCode so you will end up replacing all your whitespace with the same character. If that is OK you can use:
put replaceText(field "myTextField", "\sre", " ha") into field "myTextField"

Related

Applescritp Excel to Find And Replace "

I need an applescript to find " and replace with just a space. I found an awesome script on here that works beautifully to find and replace whole words (hello, world) but when you manipulate the script to find " and replace with a space, the " corrupts the code and it no longer works. I am hoping somebody knows a way to alter this code to make it do what I want or have an other idea. here is the code (code credit goes to adamh):
searchAndReplaceTextInCells("hello", "world")
on searchAndReplaceTextInCells(search_str, replace_str)
tell application "Microsoft Excel"
set search_range to range "A:Z"
set all_found_ranges to {} -- store for the ranges, to manipulate after searching
set found_range to ""
set counter to 0
try
set found_range to find search_range what search_str with match case
on error
log ("No matches found")
end try
if (found_range is not "") then
set first_cell_address to (get address of the cells of found_range) -- we use this to break our loop
repeat while true
set counter to counter + 1
copy found_range to end of all_found_ranges
-- Now look for next result
set found_range to find next search_range after found_range
set cell_address to (get address of the cells of found_range)
if (cell_address = first_cell_address) then
-- have looped around so we are finished!
exit repeat
end if
end repeat
end if
-- walk all the ranges found and do the string replacing
repeat with r in all_found_ranges
set value of r to my replace_chars(the value of r, search_str, replace_str)
end repeat
log ("found and replaced " & counter & " items")
end tell
end searchAndReplaceTextInCells
on replace_chars(this_text, search_string, replacement_string)
set my text item delimiters to the search_string
set the item_list to every text item of this_text
set my text item delimiters to the replacement_string
set this_text to the item_list as string
set my text item delimiters to ""
return this_text
end replace_chars
As " is a reserved character we need to treat it differently when referencing it in strings.
You could use the quote constant as your argument:
searchAndReplaceTextInCells(quote, " ")
..or you could send it in as an escaped character:
searchAndReplaceTextInCells("\"", " ")

How to hide the particular text in livecode

I want to see the particular lines where the replacements are takeing place. All other text should be in hidden format. Is it possible?
on mouseUp
put the htmlText of field "MytextField" into myHtml
set the caseSensitive to true
put the field SRText into myArrayToBe
split myArrayToBe by CR
put the number of lines of (the keys of myArrayToBe) into myArraylength
repeat with i = 1 to myArraylength
--return i
put myArrayToBe[i] into y
split y by colon
put y[1] into searchStr
put y[2] into replaceStr
if searchStr is empty then
put the 0 into m
else
replace searchStr with "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in myHtml
end if
end repeat
set the htmlText of fld "MytextField" to myHtml
end mouseUP
I'm not entirely clear on what you are looking to do, but I think you don't want to 'replace' first. Dump your myHtml into "MytextField" and then use the find syntax in that field. It will scroll to the found text and draw a square around it then you can use the foundtext, foundline, foundchunk to do replace, highlight, dim all other text or whatever your after.

How to preserve text content in livecode

I need to preserve contents before begin{text} and after end{text}.If i had two buttons named "Preserve" and "Restore". If i click Preserve button the entire text above begin{text} and after end{text} will copy to some txt file. after my editing if i click the "Restore" button the entire document which was preserved will past in the same place. Is it possible?
I am using this coding for replacement
put the htmlText of field "MytextField" into myHtml
set the caseSensitive to true
replace searchStr with "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in myHtml
set the htmlText of fld "MytextField" to myHtml
I don't have any editing in before begin{text} and after end{text}. so i can able to save time and accuracy
Probably you are looking for this:
put the htmlText of field "MytextField" into myHtml
put offset("begin{text}",myHtml) into startchar
put offset("end{text}",myHtml) into endchar
put char startchar to endchar of myHtml into text_to_preserve
put char 1 to (startchar - 1) into text_toEdit1
put char (endchar + 1) to -1 into text_toEdit2

How i replace the content between begin{document} and end{document}

How i replace the content between begin{document} and end{document}. while using my code the whole content are replaced.In below code i am using wordOffset but it's not working
on mouseUp
put wordOffset("begin{document}",fld "MytextField") into tBegin
put wordOffset("end{document}",fld "MytextField") into tEnd
put the htmlText of field "MytextField" into myHtml
--put wordOffset("begin{document}",fld "myHtml") into tBegin
--put wordOffset("begin{document}",fld "myHtml") into tEnd
set the caseSensitive to true
put the field SRText into myArrayToBe
split myArrayToBe by CR
--enable the field "SRText"
--put "red,RED" & CR & "green,GREEN" & CR & "blue,BLUE" into myArrayToBe
--split myArrayToBe by CR
put the number of lines of (the keys of myArrayToBe) into myArraylength
repeat with i = 1 to myArraylength
--return i
put myArrayToBe[i] into y
split y by colon
put y[1] into searchStr
put y[2] into replaceStr
if searchStr is empty then
put the 0 into m
else
--put (word tBegin to tEnd of fld "MytextField")
--put replaceText(word tBegin to tEnd of fld "MytextField","searchStr","good") into word tBegin to tEnd of fld "MytextField"
put wordOffset("begin{document}",fld "MytextField") into tBegin
put wordOffset("end{document}",fld "MytextField") into tEnd
--put holder into myHtml
--put replaceText(word tBegin to tEnd of fld "MytextField",searchStr,replaceStr)of fld "MytextField"
replace searchStr with "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in myHtml
end if
end repeat
--enable me
set the htmlText of fld "MytextField" to myHtml
end mouseUp
You need to use htmlText to both get and set the text, otherwise your offsets are off (e.g. if the first line of the field is bold, htmlText would give something like
<b>foo</b><br>begin{document}
whereas just 'fld "MytextField"' would give
foo
begin{document}
so offset("begin{document}",...) of the first gives 16, while for the second it gives 5. What I'd do is something like
put the htmlText of fld "MytextField" into theHTML
put offset("begin{document}", theHTML) into tBegin
put offset("end{document}", theHTML) into tEnd
add length of "begin{document}" to tBegin
put character tBegin to tEnd of theHTML into textToChange
-- now do your search/replace on textToChange, not the field
put textToChange into character tBegin to tEnd of theHTML
set the htmlText of fld "MytextField" to theHTML
I use offset() here, which gives a character offset, but wordOffset as you had it should work, too, if you keep in mind that it gives you word numbers, so you'd have to replace word tBegin to tEnd of theHTML.

Delete highlight and place only replaced string in LiveCode

The following livecode for replace the particular text and highlight the both search and replace string. The search string also strike with help of html code. Now i try to add new button that perform delete the search string and place only the replaced string and that do not highlight.
on mouseUp
put the htmlText of field "MytextField" into myHtml
set the caseSensitive to true
put the field SRText into myArrayToBe
split myArrayToBe by CR
put the number of lines of (the keys of myArrayToBe) into myArraylength
repeat with i = 1 to myArraylength
put myArrayToBe[i] into y
split y by colon
put y[1] into searchStr
put y[2] into replaceStr
if searchStr is empty then
put the 0 into m
else
replace searchStr with "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in myHtmlend if
end repeat
set the htmlText of fld "MytextField" to myHtml
end mouseUp
I cannot test your code because I do not have your field contents. But know that the command "replace" requires a container from which the strings may be referenced:
replace searchString with replaceString in fld 1
It will not throw an error, but you do not need to use "the" in such circumstances as:
put the 0 into m (just say "put 0 into m)
put the field SRText into MyArrayToBe (put field SRText into...)
You get the picture.
Anyway, write back with some test text that we might evaluate, or better, make that test yourself with simple text. Then when that works, try it on your real text.
Craig Newman

Resources