wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String.replaceAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String.replaceAll" Watch "String.replaceAll" New topic


Sylvia H Charbonneau
Ranch Hand

Joined: Jul 11, 2002
Posts: 37
Good Day !

I got around using replaceAll (by using replaceFirst), but now I'm just wondering why it didn't work (and just in case I wish to use it)

Here's the code (with the replaceAll):

Now, this code is part of the doEndTag() in a JSP page. When the variable mBody contains :

<a href="{0}">,

this code works perfectly.

When it contains :

<a href="{0}"><img src="images/next.gif" width="16" height="16" border="0" alt="<%=bundle.getString("NextPage")%>"></a>

replaceAll replaces each double quote (") with stringURL.

Now when I changed replaceAll to replaceFirst, this code works. So, my questions are:

1) Why doesn't replaceAll work, but replaceFirst does ?
2) Is there any way I could use replaceAll ?

Thank you all kindly !!


[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 27, 2004: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
It would seem that you're asking a question about regular expressions. I found that your mention of "doEndTag() in a JSP" and the example code littered with irrelevant code necessary to describe your question were distracting.

Also, you haven't described your expected results very clearly. So, I'm left to guess, or to inquire as to what results you expected. For now, I've chosen to guess.

When using replaceFirst, I doubt that your code works as well as you think it does. Are you sure you looked closely at the results?

For example, consider the following examples and results:Notice that lingering "{0}".Notice that two replacements were performed, and that {0} remains.

I've changed my mind. Instead of continuing to guess about what you're trying to do, could you please explain it? What are you trying to do? Can you give a clear example input and output? Are you trying to replace the text "{0}" with other text? Are you aware that X{n} has a special meaning when creating a pattern to match? You might like to take a look at the java.util.regex.Pattern class documentation.

Note that the pattern "{0}" is akin to the pattern ", since the "{0} is akin to the empty string - it matches where zero quotation marks are. Also, note that "{3} would match three quotation marks.

With these things in mind, consider the following example and result:Apparently, I was in the mood for a little more guesswork. Did I guess right? Have you figured it out?
[ August 27, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Sylvia H Charbonneau
Ranch Hand

Joined: Jul 11, 2002
Posts: 37
Thanks for your reply ! That hit the nail right on the head.

Sorry about the extra code. I am still new (this is the beginner section, isn't it ). However, I completely appreciate your advice, and I'll be more careful (and succinct).

I was trying to replace {0} from my JSP with a string from my custom tag. One more question. What would be good pattern to use instead of {0}

Thanks !

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Notice the last code example I posted.

You can use that pattern, if you like. You just have to keep in mind that X{n} has a special meaning, so you need to escape those special characters - X\{n\} - and then you also need to escape the escaping - X\\{n\\}}.

A while ago, I wrote a two part introduction to java.util.regex that you might like to take a look at.
  • An Introduction to java.util.regex Part II
  • An Introduction to java.util.regex Part I

  • Good luck.
    I agree. Here's the link: http://aspose.com/file-tools
    subject: String.replaceAll
    Similar Threads
    problems with replaceAll and slash character
    passed with 93% - delighted !
    duplicating String's replaceAll() method in 1.3
    Passed with 93%