Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regex match problem. 2 char string "'\''n'"

807607Nov 8 2006 — edited Nov 8 2006
Hi. How in regex may I create a pattern to catch "\n"(string length = 2) and convert it to '\n'(ASCII new line char).

For example
// user enters a string "Hello.\nMy name is foo." as a parameter
String s = getStrParam();
log.debug("Length: "+s.length()); //will output Length: 22

//so i need to convert 2 chars "\"+"n" into new line ASCII char '\n'

Pattern CRLF = Pattern.compile("[\\]n");
Matcher mtch = CRLF.matcher(s);
 
if(mtch.find()){
            log.debug("Found!");
            s=mtch.replaceAll("\n");
        }
log.debug("New string: "+m);
If I execute my code I get exception Unclosed character class near index 3 [\]n
Where is my mistake?

P.S.: I tried to verify regex pattern at http://regexlib.com/RETester.aspx and my pattern is correct.

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 6 2006
Added on Nov 8 2006
5 comments
135 views