Java: Replace String (with brackets!) -


i'm working on small program that:

  • reads excel document (.xlsx, using smartxls) , saves content in long string (a new line each cell)
  • reads config file , converts substrings in long string (replaceall) and/or adds prefix, according examples in config file
  • outputs final string txt file

example conversion: b c (with tab in between each of parts), e.g. "its[ ]bracket" -> "itsabracket" with

tagname [ ] 

in config file ("[ ]" gets replaced "a"). tag tells me, columns converted, b original substring , c string b has converted into.

the problem is: know brackets (round, curly , square) going used in config file don't know, if it's going single 1 or maybe longer phrases (with letters, digits,..) in between brackets.

string s = s.replaceall(convert[i].original, convert[i].new); 

for "[ ]" -> "a" writes [a] output file , if following first, when it's reading config file , saves in "convert" array, throws patternsyntaxexception ("unclosed character class near index 0"):

if(ss.contains("[")) {     ss = ss.replaceall("[", "\\["); } else if(ss.contains("]")) {     ss = s.replaceall("]", "\\]"); } 

with

if(ss.contains("\\[")) {     ss = ss.replaceall("\\[", "\\\\["); } else if(ss.contains("\\]")) {     ss = s.replaceall("\\]", "\\\\]"); } 

it outputs "[a]" again.

any ideas, how can make work?

use string.replace accepts character sequence unlike replaceall.you dont have use escape characters [] etc.

 string s = s.replace(convert[i].original, convert[i].new); 

input
its[][]]bracket
tagname [ ] a

output

itsaa]bracket

hope want achieve using code.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -