Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Java/Jsp String findandreplace problem

Thread view: 
questionmarc420@msn.com - 19 Jan 2006 16:35 GMT
hii,
ihave a large string that needs to be formated to html format.

now before every chart its like this

-0-
*T
text -----
*T

i need to add <pre> and </pre> tags to replace the *T's but i don't
know how to go about this.
this is the code i used to scan the file an add links

data_body = data_body.replaceAll("((https?|ftp)://|mailto:)[^\\s<]+",
"<A TARGET=\"_new\" HREF=\"$0\">$0</a>");

if anyone knows how to surround the text in <pre> tags please let me
know

thanks.
morc
Oliver Wong - 19 Jan 2006 17:24 GMT
> hii,
> ihave a large string that needs to be formated to html format.
[quoted text clipped - 15 lines]
> if anyone knows how to surround the text in <pre> tags please let me
> know

   You need to ensure that the <pre> and </pre> balance out. AFAIK, <pre>
tags cannot be nested within each other.

Replace the first occurence of "*T" you see in your string with "<pre>".
The "first" occurence is now gone, and what was your nth occurence is now
your nth-1 occurence.
Replace the first occurence of "*T" you see in your string with "</pre>".
Ditto.
Replace the first occurence of "*T" you see in your string with "<pre>".

And so on.

   - Oliver
questionmarc420@msn.com - 19 Jan 2006 17:35 GMT
thanks but i don't know how to do that. that was my question.
i don't know how to flag the second occurence. ive been using
.replaceAll method because the text is all one string.
Oliver Wong - 19 Jan 2006 17:51 GMT
> thanks but i don't know how to do that. that was my question.
> i don't know how to flag the second occurence. ive been using
> .replaceAll method because the text is all one string.

   Isn't there a "replaceFirst" method that you could use instead?

   - Oliver
questionmarc420@msn.com - 19 Jan 2006 17:59 GMT
yes. slipped my mind. too mcuh work

thanks alot i've figured out my problem
-morc
Alan Krueger - 19 Jan 2006 23:47 GMT
> -0-
> *T
[quoted text clipped - 7 lines]
> data_body = data_body.replaceAll("((https?|ftp)://|mailto:)[^\\s<]+",
> "<A TARGET=\"_new\" HREF=\"$0\">$0</a>");

This code:

    String text = "This is a test.\n" +
        "*T\n" +
        "Preformatted. * star test\n" +
        "*T\n" +
        "Not.\n" +
        "*T\n" +
        "Again!\n" +
        "*T\n" +
        "Normal.";
    text = text.replaceAll( "[*]T(([^*]|[*][^T])*)[*]T",
        "<pre>$1</pre>" );
    System.out.println( text );

Produces this result:

    This is a test.
    <pre>
    Preformatted. * star test
    </pre>
    Not.
    <pre>
    Again!
    </pre>
    Normal.

This ensures proper grouping of <pre></pre> elements by capturing both
*T occurrences, the start and end, at the same time.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.