• Post Reply Bookmark Topic Watch Topic
  • New Topic

Seaching and Replacing text program  RSS feed

 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Michael Angstadt
Ranch Hand
Posts: 278
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • 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
  • 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: 278
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • 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!"
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
551
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 37507
551
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 37507
551
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!