• 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

How to do sed search & replace in Java efficiently

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don’t the replacing methods in the String class provide what you require?
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget "s/Scoccer/Soccer/g" afterwards
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recommended reading http://docs.oracle.com/javase/tutorial/essential/regex/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic