File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to do sed search & replace in Java efficiently Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to do sed search & replace in Java efficiently" Watch "How to do sed search & replace in Java efficiently" New topic
Author

How to do sed search & replace in Java efficiently

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Dear Java Gurus,

I am wondering whether there is a way to do string search and replace similar to sed instead of using the conventional method of examining every character line by line in Java. Let’s look at an example of what I am referring to.

The file IncorrectSport.txt is made up of the following strings:

Scoccer=Wimbleton
Tennis=World Cup




I have a need to make string search and replacement to a large number of small text files and is looking for a more efficient approach to getting it done.

There is no issue with writing a simple yet dull laborious text manipulation program in Java.

Thanks in advance,

Jack
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
Don’t the replacing methods in the String class provide what you require?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Don't forget "s/Scoccer/Soccer/g" afterwards
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jack Bush wrote:
I am wondering whether there is a way to do string search and replace similar to sed instead of using the conventional method of examining every character line by line in Java.


Sed examines every character just like what you would have to write in Java. That's the most efficient way to do it (and the only reasonable way that's applicable to the general case).

Java does have a regex facility in the core API, however (which also examines every character), so you don't have to write the comparison code yourself.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Jeff,

>Java does have a regex facility in the core API, however (which also examines every character), so you don't have to write the comparison code yourself.

What is the name of this class where can I find it?

Thank you to all for your suggestion but the latter one from Jeff appears to be the one I am looking for.

Jack
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Recommended reading http://docs.oracle.com/javase/tutorial/essential/regex/


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to do sed search & replace in Java efficiently
 
Similar Threads
unable to get response from payment website
Auto Increment Row number
Problem replacing a line AWK, SED or GREP
Use of SED in shell scripts
how to read files from internet