Could someone be kind enough to suggest the most efficient way to duplicate the String class's replaceAll() method? Unfortunately, our server here at work runs 1.3 Any nudge in the right direction will be greatly appreciated.
This regExp package has been around for years and the javadocs are good.
Joined: Jun 01, 2004
Unfortunately, I don't think implementing an outside package will be an option. (Our data security dept. is extremely anal, and it would likely take an act of Congress to get this done.) I was hoping to somehow duplicate this functionality in 1.3.x using the existing API. Are regular expressions not supported until 1.4? If this is the case, maybe I am just out of luck here.
What I do currently is this:
I then store this in the db. To display in my JSP:
Couldn't be simpler. However, I have no earthly idea how to go about doing this without use of the 1.4's replaceAll(). Any help will be GREATLY appreciated
If you don't need regular expression matching, you should be able to implement a fairly generalized method to do simple replacements. Take a look at the String API docs. You should pay special attention to the indexOf() and substring() methods.
If you need the full power of regular experssions, you could try to write your own, but this seems like it's more effort than it's worth. Of course, if your place of employment is willing to foot the bill instead of upgrading their software...
Are you trying to use one particular regular expression or are you trying to add regular expressions to the toolkit at your site?
Joined: Jun 01, 2004
I'm not well-versed in the use of regular expressions. Actually, I'm not sure I even need to use a regex. I simply need to replace every occurence of a particular substring ( for example "\r\n" ) within a given String (a very long String ) with a replacement substring.
I would consider reading the target String into a StringBuffer, then search the StringBuffer for an occurance of the substring (search string). StringBuffer has methods to manipulate and rebuild the target string (lower case here is intentional to distinguish from the String object). Then, create a new String object from the StringBuffer and assign it to the original target String's variable.
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
What I'm doing is handing the method a String whose value is derived from a textarea in a JSP. It replaces "\r" with "&NEWLINE&" correctly. However, when I use my method to replace "&NEWLINE&" with <br> tags, I run into problems. For some reason it will look almost right, but some characters will be missing, some breaks will be a bit off. However, if I use my own replaceAll method to change "\r" to "&LINEFEED&" and use String class's replaceAll method to change "&LINEFEED&s" into <br> tags, it works correctly. I just can't figure out why my method doesn't change "&LINEFEED&" to <br> correctly, but it changes "\r" to "&LINEFEED&" correctly. [ November 09, 2004: Message edited by: Bill Brasskey ]
Joined: Jul 30, 2003
Hi, Bill -
For some reason it will look almost right, but some characters will be missing, some breaks will be a bit off.
What characters are missing? How are the breaks off?
Joined: Dec 06, 2001
What is the reason to do this in two steps? Can you simply replace "\n\r" directly with "<BR>" rather than using the intermediate "&NEWLINE&"? (Or is it "&LINEFEED&"? I'm confused on this detail.)