How can I replace a substring of an AttributedString with another string of characters? I have an application that allows the user to input text into a JTextPane and the text is then stored as an AttributedString. I'd like them to be able to type in something like "My name is %NAME%" and then be able to replace the characters %NAME% with a string from a database. I've been trying to use regular expressions to do this but the problem is that I need to be able to retain the character formatting. I can strip all the character attributes off and get String containing the characters of the AttributedString and then use a regx to replace the substring but then I have to put the attributes back on which is especially difficult if the length of the two substrings is different. i.e. %NAME% is 6 characters, Bob is 3. Any suggestions?