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
Related
I completed code to remove any data in front of a string, add some text (with a space) to the front and store it back in the cell.
However, every time I run the macro (to check if changes that I've made are working for example), a new space is added in between the words.
The code that removes anything before the name and adds the required string. I have called a InStr function and stored the value in integer pos. Note that this is in a loop over a specific range.
If pos > 0 Then
'Removes anything before the channel name
cellValue.Offset(0, 2) = Right(cell, Len(cell) - InStr(cell, pos) - 2)
'Add "DA" to the front of the channel name
cellValue.Offset(0, 0) = "DA " & Right(cell, Len(cell) - InStr(cell, pos) - 2)
'Aligns the text to the right
cellValue.Offset(0, 2).HorizontalAlignment = xlRight
End If
An additional "DA" is not being added and I haven't made any other functions to add spaces anywhere. The extra space is not added if adding "DA " is changed to "DA".
I'd prefer not to add another function/sub/something somewhere to search and remove any extra spaces.
What the string is AND what is in front of the string is unknown. It could be numbers, characters, spaces or exactly what I want it to be. For example, it could be "Q-Quincey", "BA Bob", "DA White" etc. I thought that searching through the cell for the string I want (Quincey, Bob, White) and altering the cell as needed would be the best way.
Solution that you all helped me come up with:
If pos > 0 Then
modString = Right(cell, Len(cell) - InStr(cell, pos) - 2)
'Removes anything before the channel name and places it in the last column
cellValue.Offset(0, 2) = modString
'Aligns the last column text to the right
cellValue.Offset(0, 2).HorizontalAlignment = xlRight
cellValue.Offset(0, 2).Font.Size = 8
'Add "DA" to the front of the channel name in the rightmost column
If StartsWith(cell, "DA ") = True Then
cellValue.Replace cell, "DA" & modString
Else
cellValue.Replace cell, "DA " & modString
End If
End If
Maybe this is something you can work with:
Sample data:
Sample code:
Sub Test()
With Sheet1.Range("A1:A4")
.Replace "*quincey", "AD Quincey"
End With
End Sub
Result:
In your examples, it seems you want to replace the first "word" in the string with something else. If that is always the case, the following function, which makes use of Regular Expressions, can do that:
Option Explicit
Function replaceStart(str As String, replWith As String) As String
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = "^\S+\W(?=\w)"
replaceStart = .Replace(str, replWith)
End With
End Function
Sub test()
Debug.Print replaceStart("Q-Quincy", "DA ")
Debug.Print replaceStart("BA Bob", "DA ")
Debug.Print replaceStart("DA White", "DA ")
End Sub
The debug.print will -->
DA Quincy
DA Bob
DA White
The regular expression matches everything up to but not including the first "word" character that follows a non-word character. This should be the second word in the string.
A "word" character is anything in the set of [A-Za-z0-9_]
Seems to work on the examples you present.
If you wanted to go about it through a loop you should remove some redundancies in your code. For instance, refering to cell.offset(0,0) doesn't make sense.
I would set the target cells to a range and simply edit that cell with out placing the unwanted strings in another cell.
**EDIT:
I'd try something like this.**
nameiwant = "Quincy"
Set cell = Range("A1")
If InStr(cell, nameiwant) > 0 And Left(cell, 3) <> "DA " Then
cell.Value = "DA " & nameiwant
End If
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
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.
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"