• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Seaching and Replacing text program

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Please help me in writing a program in Core Java for searching a particular line of text from a large file and then replacing that instance of the text with another text. I have thought about the following things :

1) using regular expressions to search the text and then replace the text.

2) using file handling strategy to search the file till the end of file is reached and then replacing the text when it is found.

Please give me the ideas for both of these or any other strategy that can be used.

Thanks.
 
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It isn't that easy; maybe easier to read part of the file, copy it, then find the bit to change, change and copy that, then copy the remainder of the file. Then delete the original. Remember a file has to be read right through to alter something in the middle.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what should I do to implement this (search/replace) utility in Core Java. Please give me some ideas.

Thanks.
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If your search string can span multiple lines, then you'll run into trouble, but otherwise this should work.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes your guess is right my search string spans about 3 - 4 lines. What should I do now. Which strategy should I now adopt? Please help.

Thanks.
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siddharth Bhargava wrote:Yes your guess is right my search string spans about 3 - 4 lines. What should I do now. Which strategy should I now adopt? Please help.

Thanks.



This will work, but if there is text you want to replace at the very beginning of the file, then it will remove and not replace that text. Anyone know how you might get around this using Scanner?

regex="foo"
replacement="bar"
input="fooHello world foo!"
output="Hello world bar!"
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siddharth Bhargava wrote:Yes your guess is right my search string spans about 3 - 4 lines. What should I do now. Which strategy should I now adopt? Please help. Thanks.


If the file isn't too huge, you can just read in the whole file, do the replace with a single regular expression (be sure to use Pattern.DOT_ALL mode) and output the whole file.
 
Jeanne Boyarsky
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,
I don't know how to get scanner to match endpoints either, but I tried it. The code doesn't match if "foo" is at the end of the string either.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "load it all into memory" was my approach. Here is the code, for what it is worth. It's not great but it works:

If your target string has a size that can be determined (i.e. no wildcards in the regex) then you could just read in the first block of characters of that size into some kind of buffer, compare, if no match found write the first char out, shuffle everything forward one char, read in next char, compare again. As soon as you get a match, write the whole replacement to the output, clear the buffer and read a full block in. Repeat until EOF.

As for the Scanner and endpoints: I think because they are delimiters, there is nothing before the first one nor after the last as far as the Scanner is concerned. But I'd need to read-up again on regex etc (I seem to recall there are a few rules around endpoints and null matches etc).
 
Jeanne Boyarsky
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:As for the Scanner and endpoints: I think because they are delimiters, there is nothing before the first one nor after the last as far as the Scanner is concerned. But I'd need to read-up again on regex etc (I seem to recall there are a few rules around endpoints and null matches etc).


This I know how to do, but it's so complex it's not even worth it. You could use a positive lookahead to match the beginning of the string followed by the desired reg exp. And similarly for the end with negative lookbehind.
 
Well THAT's new! Comfort me, reliable tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic