Java/Jsp String findandreplace problem

Q

questionmarc420

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
 
O

Oliver Wong

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

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
 
Q

questionmarc420

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.
 
O

Oliver Wong

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
 
Q

questionmarc420

yes. slipped my mind. too mcuh work

thanks alot i've figured out my problem
-morc
 
A

Alan Krueger

-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>");

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top