• Post Reply Bookmark Topic Watch Topic
  • New Topic

replace last occurrence of string in file

 
peter m hayward
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a book entry system that used a number of text boxes to collect the data about the book, title author etc, once all the text boxes are filled the data is sent to mysql and also a text file for upload to a website
The possibility of an error means i need to be able to effectively edit the text file bearing in mind that last entry in the file is that of the current book
What i have so far is this

ideally i need content = content.replacelast  which does not exist then the error that has just taken place will be corrected so how can i convert content.replaceFirst to  content.replacelast

 
Henry Wong
author
Sheriff
Posts: 22644
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
peter m hayward wrote:
ideally i need content = content.replacelast  which does not exist then the error that has just taken place will be corrected so how can i convert content.replaceFirst to  content.replacelast


Well, one option would be to greedily grab everything (or as much as possible) before "pink elephant", leaving only the last "pink elephant" to be replaced. Sort of something like this...
Henry
 
Rob Spoor
Sheriff
Posts: 20837
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using $ to indicate the end of the string? I think you would need to add (?s) (Pattern.DOTALL) to the regex though:
(untested)
 
Henry Wong
author
Sheriff
Posts: 22644
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:How about using $ to indicate the end of the string?


This would work only if the last occurrence is in the last position. It will not work if there is anything else after the last occurrence.

Henry
 
peter m hayward
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:How about using $ to indicate the end of the string? I think you would need to add (?s) (Pattern.DOTALL) to the regex though:
(untested)


having read your reply it gave me an idea which seems to work here is the code just in case it helps some else


this removes the last entry into the file as it moves the pointer that defines the end of the file, it detects CR ascii 13 place at the end of the previous file
 
Rob Spoor
Sheriff
Posts: 20837
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Rob Spoor wrote:How about using $ to indicate the end of the string?


This would work only if the last occurrence is in the last position. It will not work if there is anything else after the last occurrence.

Henry

You're right of course.
 
Junilu Lacar
Marshal
Posts: 10411
125
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I understand is that you'd like to roll back your changes to a text file if something goes wrong with writing to the database, correct? Then why don't you just cut to the chase and work at the file level instead? Either use a temp file for the transaction and only make it "official" when the transaction commits successfully or treat the previous version as a backup and revert to it on rollback. There are other options along this vein but that's the general idea.
 
Junilu Lacar
Marshal
Posts: 10411
125
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also make the write to the text file contingent on a successful write to the database. I think that would make the logic of ensuring the text file is in synch with the database simpler.
 
peter m hayward
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:What I understand is that you'd like to roll back your changes to a text file if something goes wrong with writing to the database, correct? Then why don't you just cut to the chase and work at the file level instead? Either use a temp file for the transaction and only make it "official" when the transaction commits successfully or treat the previous version as a backup and revert to it on rollback. There are other options along this vein but that's the general idea.


I posted the code so that others can benefit, the system has to create 3 different thing a mysql entry and two texts file with different layouts, i have now got this to work by just deleting the last entry to the text files and running a mysql update

thanks for trying to help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!