Mark Clarke on Thu, 06 Mar 2003 08:54:40 +0200 (SAST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[Linux dev] Find replace sing regular expressions


Hi All,

I want to read in a string of characters find a pattern in the string
and then replace the pattern before writing out the string again. I am
using the Pattern and Matches class to do this but it seem that neither
of these classes give you access to the string that you are replacing
i.e. that matches your pattern.

E.G.
Say I have the following line that I read in from a config file. It
tells me that these are the fields that I need to write out from a SQL
query.

{customerName},{Address},{Balance}

String str = "{customerName},{Address},Your balance is {Balance}";
Pattern p = Pattern.compile("{.*}");
Matches m = p.matches(str);
StringBuffer tmpStr = new StringBuffer();
While (m.find()){
M.appendReplace(tmpStr,"X");
}
M.appendTail(tmpStr);



What I want to do is something like

String str = "{customerName},{Address},Your balance is {Balance}";
Pattern p = Pattern.compile("{.*}");
Matches m = p.matches(str);
StringBuffer tmpStr = new StringBuffer();
While (m.find()){
String tmp = M.getMatchedString();
//rs = some recordset
Tmp= Rs.get(tmp.replace('{','');
M.appendReplace(tmpStr,tmp);
}
M.appendTail(tmpStr);


I cant seem to find a way to access the set of matched string anywhere.

Thanks
Mark


=========================================
Mark Clarke - Exotic Pets
E-Mail:  Mark@xxxxxxxxxxxxxxxx
WWW:     www.ExoticPets.co.za
=========================================