
This is the code i ended up with. The context is that i don't control the lines (so i can't use a regex - in don't know if i have all the input). I suppose to be sure i should employ a stringbuilder to concatenate the lines while they don't match the end of the "tag". boolean isMarkupStart = line.startsWith("\n***"); if (isMarkupStart) { isStart = line.contains(START_TAG); isEnd = line.contains(END_TAG); inTag = isStart || isEnd; } if (isInValidText && !inTag) { super.insertString(offset, line, attr); } //best i can do. If the string breaks exactly at *\n** //or something, i suppose this wil break horribly. if (inTag && line.endsWith("***")) { isInValidText = isStart && !isEnd; inTag = false; }