From: Andy H. <an...@jt...> - 2004-12-14 04:10:35
|
Lenny G Arbage wrote: > I could use some help expanding strings with text in > them. If I understand correctly, your second replaceAll() needs to match on a pure literal string, with no regex pattern matching behavior. If this is correct it would be better to use the findAndReplace() function of class UnicodeString. /** * Replace all occurrences of characters in oldText with the * characters in newText * * @param oldText the text containing the search text * @param newText the text containing the replacement text * @return a reference to this * @stable ICU 2.0 */ inline UnicodeString& findAndReplace(const UnicodeString& oldText, const UnicodeString& newText); For the second problem, why you're getting garbage from from the replaceAll() of backslashes, I'm not sure. It looks like the replace string needs two more back slashes, but that shouldn't have resulted in garbage output. Even with the backslashes working, you would still need to worry about other characters with special meaning in regex patterns, such as . * + ^ or $. -- Andy Heninger hen...@us... > > Here's the problem: > UnicodeString input = "path1=C:\somedir\somefile, > path2=D:\temp"; > UnicodeString matchpattern = "path.=(.*), path.=(.*)". > > > Iterating through the RegexMatcher::group()s gives the > expected: > m1 == "C:\somedir\somefile" > m2 == "D:\temp" > > But now I want to use these UnicodeStrings as replace > strings in a new string, i.e., I want to call > RegexMatcher.replaceAll(m1,..) and replaceAll(m2,..), > searching through an input string like "p1=\1, p2=\2" > and making the substitutions. > > Of course, since these strings are now interpreted as > patterns, '\s' and '\t' get turned into something > else. I've tried doing a replaceAll on them before > hand, turning all '\' into '\\', but I'm missing > something. How do I fix the following to do the right > thing? > > RegexMatcher backslashMatcher("\\\\", 0, statcode); > backslashMatcher.reset(m1,statcode); > UnicodeString irep = backslashMatcher.replaceAll( > UNICODE_STRING_SIMPLE("\\\\\\"), statcode); > > As is, this results in garbage like > "픈䀰temp" instead of the expected > "D:\\temp". > > Any help would be appreciated! > > Lenny G > > BTW, the approach I described above is working for > strings that don't have '\'s in them. > > > |