W wolfray1972 Member Licensed User Longtime User Oct 29, 2020 #1 Hello all, I have a textfile with a line like this: .1.|Hello world |00000 Now my question is how to replace "Hello world" to lets say "bye bye world" Hope you could help me.
Hello all, I have a textfile with a line like this: .1.|Hello world |00000 Now my question is how to replace "Hello world" to lets say "bye bye world" Hope you could help me.
R rosippc64a Active Member Licensed User Longtime User Oct 29, 2020 #2 ".1.|Hello world |00000".Replace("Hello world ","bye bye world") Upvote 0
R rosippc64a Active Member Licensed User Longtime User Oct 29, 2020 #3 text = RegexReplace("(.*)\|(.*)\|(.*)", line, "$1|bye bye world|$3") not tested, but looks like this B4X: Copy Sub RegexReplace(Pattern As String, Text As String, Replacement As String) As String Dim m As Matcher m = Regex.Matcher(Pattern, Text) Dim r As Reflector r.Target = m Return r.RunMethod2("replaceAll", Replacement, "java.lang.String") End Sub Upvote 0
text = RegexReplace("(.*)\|(.*)\|(.*)", line, "$1|bye bye world|$3") not tested, but looks like this B4X: Copy Sub RegexReplace(Pattern As String, Text As String, Replacement As String) As String Dim m As Matcher m = Regex.Matcher(Pattern, Text) Dim r As Reflector r.Target = m Return r.RunMethod2("replaceAll", Replacement, "java.lang.String") End Sub
klaus Expert Licensed User Longtime User Oct 29, 2020 #4 Or with B4X code: B4X: Copy Private str = ".1.|Hello world |00000" As String Private i1, i2 As Int i1 = str.IndexOf("|") i2 = str.IndexOf2("|", i1 + 1) str = str.Replace(str.SubString2(i1 + 1, i2), "New text") Upvote 0
Or with B4X code: B4X: Copy Private str = ".1.|Hello world |00000" As String Private i1, i2 As Int i1 = str.IndexOf("|") i2 = str.IndexOf2("|", i1 + 1) str = str.Replace(str.SubString2(i1 + 1, i2), "New text")
Erel B4X founder Staff member Licensed User Longtime User Oct 29, 2020 #5 Note that the RegexReplace sub is no longer needed. It is available with Regex.Replace. Upvote 0
W wolfray1972 Member Licensed User Longtime User Oct 29, 2020 #6 Thank you al for your awnser, I tried it and it works if i use Klaus his code. But what if i do no know what is between "| | " I mean what if Hello World contains other text. I know i could replace "Hello world" with Replace. But what i the Text is Unknown. But always between .1.| "Text here" |. Regards, Ray Upvote 0
Thank you al for your awnser, I tried it and it works if i use Klaus his code. But what if i do no know what is between "| | " I mean what if Hello World contains other text. I know i could replace "Hello world" with Replace. But what i the Text is Unknown. But always between .1.| "Text here" |. Regards, Ray
M Mahares Expert Licensed User Longtime User Oct 29, 2020 #7 wolfray1972 said: But what i the Text is Unknown. But always between .1.| "Text here" |. Click to expand... Maybe in that case you can use RegEx.split: Code: Copy Dim s As String =$".1.|Hello world |00000"$ Dim sarray() As String =Regex.Split("\|",s) 'need to escape with \ s =s.Replace(sarray(1), "bye bye world") Upvote 0
wolfray1972 said: But what i the Text is Unknown. But always between .1.| "Text here" |. Click to expand... Maybe in that case you can use RegEx.split: Code: Copy Dim s As String =$".1.|Hello world |00000"$ Dim sarray() As String =Regex.Split("\|",s) 'need to escape with \ s =s.Replace(sarray(1), "bye bye world")
W wolfray1972 Member Licensed User Longtime User Oct 29, 2020 #8 Hello Mahares, Thank you for your reply. But using s as string it will always look for that line of text. I just need to place a different text between the two "|" charachters. Upvote 0
Hello Mahares, Thank you for your reply. But using s as string it will always look for that line of text. I just need to place a different text between the two "|" charachters.
klaus Expert Licensed User Longtime User Oct 29, 2020 #9 Have you test Mahares' code, it works ! sarray(1) holds the string between the two | |! Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Oct 29, 2020 #10 Another option that is useful when you need to make a regex based replacement with logic: (based on: https://www.b4x.com/android/forum/threads/csbuilder-marking-based-on-regex-pattern.83002/#content) Code: Copy Sub ReplacePattern(Input As String, Pattern As String, GroupNumber As Int) As String Dim sb As StringBuilder sb.Initialize Dim lastMatchEnd As Int = 0 Dim m As Matcher = Regex.Matcher(Pattern, Input) Do While m.Find Dim currentStart As Int = m.GetStart(GroupNumber) sb.Append(Input.SubString2(lastMatchEnd, currentStart)) lastMatchEnd = m.GetEnd(GroupNumber) 'apply the replacement here: Dim val As String = m.Group(GroupNumber) For i = val.Length - 1 To 0 Step - 1 sb.Append(val.CharAt(i)) Next Loop If lastMatchEnd < Input.Length Then sb.Append(Input.SubString(lastMatchEnd)) Return sb.ToString End Sub Code: Copy Dim s As String =$".1.|Hello world|asdasd"$ s = ReplacePattern(s, "\|([^|]+)\|", 1) Log(s) output: .1.|dlrow olleH|asdasd Upvote 0
Another option that is useful when you need to make a regex based replacement with logic: (based on: https://www.b4x.com/android/forum/threads/csbuilder-marking-based-on-regex-pattern.83002/#content) Code: Copy Sub ReplacePattern(Input As String, Pattern As String, GroupNumber As Int) As String Dim sb As StringBuilder sb.Initialize Dim lastMatchEnd As Int = 0 Dim m As Matcher = Regex.Matcher(Pattern, Input) Do While m.Find Dim currentStart As Int = m.GetStart(GroupNumber) sb.Append(Input.SubString2(lastMatchEnd, currentStart)) lastMatchEnd = m.GetEnd(GroupNumber) 'apply the replacement here: Dim val As String = m.Group(GroupNumber) For i = val.Length - 1 To 0 Step - 1 sb.Append(val.CharAt(i)) Next Loop If lastMatchEnd < Input.Length Then sb.Append(Input.SubString(lastMatchEnd)) Return sb.ToString End Sub Code: Copy Dim s As String =$".1.|Hello world|asdasd"$ s = ReplacePattern(s, "\|([^|]+)\|", 1) Log(s) output: .1.|dlrow olleH|asdasd
W wolfray1972 Member Licensed User Longtime User Oct 31, 2020 #11 Hi all, I have been trying alot. I going to try to show you what i want to do. I have a Log.dat file: I need to insert new data from a scanner and replace "%black" and 000001.00 . This file can contain 1 line or 50 lines. New data is recieved in a listview. My intention is to add listview item(1) "%Pink" and replace that at line 1 with "%black" And listview2.item(1) will be replacing 000001.00 And so on. Loop till done I am trying this with the following code: B4X: Copy Dim List1 As String List1 = File.ReadString(File.DirDefaultExternal, "Log.dat") Then i split B4X: Copy Dim sarray() As String =Regex.Split("\|",List1) With this i get everything between the "|" like Mahares example. But i also need the numbers between the last "|" and the NUL. So i tried the following, what i thought was good. But it wasnt. Where could i found more info about "\|([^|]+)\|" and how to use it? B4X: Copy Dim sarray() As String =Regex.Split("\|([^|]+)\|",List1) The file is created by a external system, and i cannot acces that system. It gives some kind of signature on the .dat file. When i write a new .dat file it doesnt work. If i replace the characthers between "|" it works. That why i need to do it like this. I hope this is better to understand then my first question. Regards, Ray Upvote 0
Hi all, I have been trying alot. I going to try to show you what i want to do. I have a Log.dat file: I need to insert new data from a scanner and replace "%black" and 000001.00 . This file can contain 1 line or 50 lines. New data is recieved in a listview. My intention is to add listview item(1) "%Pink" and replace that at line 1 with "%black" And listview2.item(1) will be replacing 000001.00 And so on. Loop till done I am trying this with the following code: B4X: Copy Dim List1 As String List1 = File.ReadString(File.DirDefaultExternal, "Log.dat") Then i split B4X: Copy Dim sarray() As String =Regex.Split("\|",List1) With this i get everything between the "|" like Mahares example. But i also need the numbers between the last "|" and the NUL. So i tried the following, what i thought was good. But it wasnt. Where could i found more info about "\|([^|]+)\|" and how to use it? B4X: Copy Dim sarray() As String =Regex.Split("\|([^|]+)\|",List1) The file is created by a external system, and i cannot acces that system. It gives some kind of signature on the .dat file. When i write a new .dat file it doesnt work. If i replace the characthers between "|" it works. That why i need to do it like this. I hope this is better to understand then my first question. Regards, Ray
E emexes Expert Licensed User Longtime User Oct 31, 2020 #12 wolfray1972 said: It gives some kind of signature on the .dat file. When i write a new .dat file it doesnt work. Click to expand... Could you show us: 1/ a pre-substitution log.dat file with more sample lines (doesn't have to be thousands; six would be great) 2/ a post-substitution log.dat file, ideally for the same sample and, even better, one that works Last edited: Oct 31, 2020 Upvote 0
wolfray1972 said: It gives some kind of signature on the .dat file. When i write a new .dat file it doesnt work. Click to expand... Could you show us: 1/ a pre-substitution log.dat file with more sample lines (doesn't have to be thousands; six would be great) 2/ a post-substitution log.dat file, ideally for the same sample and, even better, one that works
E emexes Expert Licensed User Longtime User Oct 31, 2020 #13 wolfray1972 said: The file is created by a external system, and i cannot access that system. It gives some kind of signature on the .dat file. When i write a new .dat file it doesnt work. Click to expand... Another name for a log file is an audit trail, and it is entirely reasonable and normal for audit trails to have a signature on them to detect alterations. If that signature prevents your changes from being accepted, aren't we are on a hiding to nothing here anyway? Although it's still an interesting programming question, eg, subtleties such as what if there are "overlapping" names such as %black and %blackdog and %blackpudding, and what to do about alignment ie change in line and field length. Upvote 0
wolfray1972 said: The file is created by a external system, and i cannot access that system. It gives some kind of signature on the .dat file. When i write a new .dat file it doesnt work. Click to expand... Another name for a log file is an audit trail, and it is entirely reasonable and normal for audit trails to have a signature on them to detect alterations. If that signature prevents your changes from being accepted, aren't we are on a hiding to nothing here anyway? Although it's still an interesting programming question, eg, subtleties such as what if there are "overlapping" names such as %black and %blackdog and %blackpudding, and what to do about alignment ie change in line and field length.
W wolfray1972 Member Licensed User Longtime User Nov 1, 2020 #14 If i change the%black in a txt editor and the numbers 000001.00 to 000002.00 and save the file. Its works. If i write new file like: " .2.| " & listview1.item(1) & "|" & listview2.item(1) & chr(0) and save as Log.dat it is not working. The system reads the file, but not process it. I attached the .dat file. Only changes extension to txt else upload not possible Attachments Log.txt Log.txt 296 bytes · Views: 563 Last edited: Nov 1, 2020 Upvote 0
If i change the%black in a txt editor and the numbers 000001.00 to 000002.00 and save the file. Its works. If i write new file like: " .2.| " & listview1.item(1) & "|" & listview2.item(1) & chr(0) and save as Log.dat it is not working. The system reads the file, but not process it. I attached the .dat file. Only changes extension to txt else upload not possible
E emexes Expert Licensed User Longtime User Nov 1, 2020 #15 Post three sample files like that log.txt: 1/ before changes (as received from the inaccessible system) 2/ after manual changes, and working 3/ after changes by program, and not working (?) and I can probably tell you where the changes-by-program is going awry. Call them log1.txt, log2.txt and log3.txt per the above numbering. My first guess is that the Chr(0) is doing no good. Upvote 0
Post three sample files like that log.txt: 1/ before changes (as received from the inaccessible system) 2/ after manual changes, and working 3/ after changes by program, and not working (?) and I can probably tell you where the changes-by-program is going awry. Call them log1.txt, log2.txt and log3.txt per the above numbering. My first guess is that the Chr(0) is doing no good.
Erel B4X founder Staff member Licensed User Longtime User Nov 1, 2020 #16 emexes said: My first guess is that the Chr(0) is doing no good. Click to expand... True. No place for such character in a text file. Why aren't you using StringUtils.SaveCSV? Or B4XSerializator? Or KeyValueStore? There are such simple ways to store lists or tables, why work hard? Upvote 0
emexes said: My first guess is that the Chr(0) is doing no good. Click to expand... True. No place for such character in a text file. Why aren't you using StringUtils.SaveCSV? Or B4XSerializator? Or KeyValueStore? There are such simple ways to store lists or tables, why work hard?
W wolfray1972 Member Licensed User Longtime User Nov 1, 2020 #17 Thank you for your awnsers again. I attached the files as you asked. I can see no difference in the files. Yet it reads. the .dat file just does not process it. The reason i use chr(0). is that i tried the �� as a write line. But then it doesnt show as NUL. If i use chr(0) it does show as NUL. The program i read and edit the .dat file with is notepad++. Maybe i taking this al wrong. Like Erel is saying. But i have no qlue how to take this a otherway. I had the simple thought of counting the lines in the .dat file and then count the lines from listviews. Replace the existing lines with listview items and delete in .dat file the lines that are not in the listview. listview.items = 4 .dat lines = 5 Replace .dat file lines 1 to 4 with listview items. Then delete line 5. save file. All done!! But nooooo. I will check: Why aren't you using StringUtils.SaveCSV? Or B4XSerializator? Or KeyValueStore? Regards, Ray Attachments Log1.txt Log1.txt 205 bytes · Views: 469 Log2.txt Log2.txt 205 bytes · Views: 494 Log3.txt Log3.txt 106 bytes · Views: 473 Upvote 0
Thank you for your awnsers again. I attached the files as you asked. I can see no difference in the files. Yet it reads. the .dat file just does not process it. The reason i use chr(0). is that i tried the �� as a write line. But then it doesnt show as NUL. If i use chr(0) it does show as NUL. The program i read and edit the .dat file with is notepad++. Maybe i taking this al wrong. Like Erel is saying. But i have no qlue how to take this a otherway. I had the simple thought of counting the lines in the .dat file and then count the lines from listviews. Replace the existing lines with listview items and delete in .dat file the lines that are not in the listview. listview.items = 4 .dat lines = 5 Replace .dat file lines 1 to 4 with listview items. Then delete line 5. save file. All done!! But nooooo. I will check: Why aren't you using StringUtils.SaveCSV? Or B4XSerializator? Or KeyValueStore? Regards, Ray
E emexes Expert Licensed User Longtime User Nov 1, 2020 #18 wolfray1972 said: I attached the files as you asked. Click to expand... ??? wolfray1972 said: I can see no difference in the files. Click to expand... Without even looking at contents, can see that the third file must be different. Upvote 0
wolfray1972 said: I attached the files as you asked. Click to expand... ??? wolfray1972 said: I can see no difference in the files. Click to expand... Without even looking at contents, can see that the third file must be different.
E emexes Expert Licensed User Longtime User Nov 1, 2020 #20 wolfray1972 said: Why? Click to expand... File size is half that of the other two files. Upvote 0