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 regular expression regex to replace contains() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "regular expression regex to replace contains() " Watch "regular expression regex to replace contains() " New topic
Author

regular expression regex to replace contains()

jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hi ... how can I replace multiple "contains" calls with a regex in java ?

i.e.


Please let me know... I think this will be usefull for a lot of intermediate regex users...
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26192
    
  66

Jay,
There might be a simpler way to do it, but this works:


I think the contains approach is clearer though.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
There has to be a simpler way...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56192
    
  13

Originally posted by jay vas:
There has to be a simpler way...

I agree with Jeanne that contains() will always be simpler than a regex.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by jay vas:
There has to be a simpler way...


How do you come to that conclusion?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26192
    
  66

My note about a simpler way was more that I didn't spend any time tuning the reg exp. I wrote the first thing I thought of that is equivalent. In practice, I would just use contains. There is no reason to believe the reg exp version would be more efficient anyway. So why sacrifice readability?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: regular expression regex to replace contains()
 
Similar Threads
String parsing help.
Find/replace using RegEx in JBuilder 2008
CSS Float - how to "turn off" for subsequent content
backslash n is being escaped against my will
Print String from an Array