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.
Related
I'm trying to clean up the string so that it removes "//name#" and "deletethispart.file" from "//name#main.domain.com/directory01/directory02/deletethispart.file"
set mainString to "//name#main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name#", "deletethispart.file"}
repeat with i from 1 to count of cleanUpTerms
set text item delimiters to item i of cleanUpTerms
set cleanURL to text items of mainString
set text item delimiters to ""
set mainString to cleanURL
end repeat
It's not working :(
You have to concatenate the text items at the end of the repeat loop.
Renaming cleanURL with textItems makes it clearer
set mainString to "//name#main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name#", "deletethispart.file"}
repeat with i from 1 to count of cleanUpTerms
set text item delimiters to item i of cleanUpTerms
set textItems to text items of mainString
set text item delimiters to ""
set mainString to textItems as text
end repeat
As red_menace said, text item delimiters can be a list. it means you do not need a loop for each delimiter:
set mainString to "//name#main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name#", "deletethispart.file"}
set text item delimiters to cleanUpTerms
set cleanURL to text items of mainString
set text item delimiters to ""
set mainString to cleanURL as text
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.
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
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"
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