• 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

String.replaceAll

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!

S

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 27, 2004: Message edited by: Dirk Schreckmann ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sylvia H Charbonneau
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !

S
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic