• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

replace last occurrence of string in file

 
Ranch Hand
Posts: 164
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 164
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 164
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic