Android Question Replace characters between two characters in a text file.

wolfray1972

Member
Licensed User
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.
 

rosippc64a

Active Member
Licensed User
Longtime User
text = RegexReplace("(.*)\|(.*)\|(.*)", line, "$1|bye bye world|$3")
not tested, but looks like this

B4X:
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

klaus

Expert
Licensed User
Longtime User
Or with B4X code:

B4X:
    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

wolfray1972

Member
Licensed User
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

Mahares

Expert
Licensed User
Longtime User
But what i the Text is Unknown. But always between .1.| "Text here" |.
Maybe in that case you can use RegEx.split:
B4X:
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

Member
Licensed User
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

Erel

B4X founder
Staff member
Licensed User
Longtime User
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)

B4X:
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

B4X:
Dim s As String =$".1.|Hello world|asdasd"$
s = ReplacePattern(s, "\|([^|]+)\|", 1)
Log(s)

output: .1.|dlrow olleH|asdasd
 
Upvote 0

wolfray1972

Member
Licensed User
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:

Capture.PNG



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:
    Dim List1 As String
    List1 = File.ReadString(File.DirDefaultExternal, "Log.dat")

Then i split

B4X:
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:
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

emexes

Expert
Licensed User
It gives some kind of signature on the .dat file. When i write a new .dat file it doesnt work.
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:
Upvote 0

emexes

Expert
Licensed User
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.
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

Member
Licensed User
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
    296 bytes · Views: 220
Last edited:
Upvote 0

emexes

Expert
Licensed User
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

wolfray1972

Member
Licensed User
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
    205 bytes · Views: 171
  • Log2.txt
    205 bytes · Views: 182
  • Log3.txt
    106 bytes · Views: 182
Upvote 0
Top