Home arrow static arrow Java Programming [Archive] - Help in string handling
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Help in string handling
This topic has 5 replies on 1 page.

Posts:16
Registered: 4/29/04
Help in string handling  
Jun 8, 2004 11:33 PM



 
I have the following string fro example:

<start>
<xxx><start><yyy>
<xxx><start att1="1" att2="2">

If I want the above string to become:
<start/>
<xxx><start/><yyy>
<xxx><start att1="1" att2="2"/>

which mean that for every element of <start> , at the end of the tag I want to replace '>' become ' />'
so i mean to search for the pattern "*<start*>*", which I think it is correct, but i am not sure how to convert this matching pattern into replacement like this "*<start*/>"(i tried this but does not work).

I use:
String thisLine;
....

thisLine.replaceAll("*<START*>*", "*<ISEMPTY*/>*");
it end up like this:

<START attribute1="value1"> become *<START attribute1="value1"/>

Can anyone help me on this?
 

Posts:342
Registered: 5/22/98
Re: Help in string handling  
Jun 9, 2004 12:40 AM (reply 1 of 5)



 
ChoonKia,

try
thisLine = thisLine.replaceAll("<([(start)(START)].*?)>", "<$1/>");


Take a look at the java.util.regex.Pattern and java.util.regex.Matcher javadoc pages for the regular expression syntax.

Graeme
 

Posts:342
Registered: 5/22/98
Re: Help in string handling  
Jun 9, 2004 12:45 AM (reply 2 of 5)



 
Sorry, that line should read

thisLine = thisLine.replaceAll("<((start|START).*?)>", "<$1/>");


Didn't check my own syntax :-)

Graeme
 

Posts:16
Registered: 4/29/04
Re: Help in string handling  
Jun 9, 2004 2:45 AM (reply 3 of 5)



 
What if I have such string

<end>
if I run this thisLine = thisLine.replaceAll("<((start|START).*?)>", "<$1/>");
it will end up with <end/> right?

also

if I have <start><end>,
if will still end up with
<start/><end/>

although i only need
<end> remain unchange
and <start><end> become <start/><end>

any way to solve this?
 

Posts:16
Registered: 4/29/04
Re: Help in string handling  
Jun 9, 2004 7:55 PM (reply 4 of 5)



 
can some one help me in this regular expression?
 

Posts:342
Registered: 5/22/98
Re: Help in string handling  
Jun 15, 2004 1:03 AM (reply 5 of 5)



 
Choonkia,

The expression I gave you should result in <start/><end>, not <start/><end/>. Have you tried the code, or are you just surmising?

Graeme
 
This topic has 5 replies on 1 page.