5 Replies Latest reply: Nov 8, 2006 11:07 AM by 807607 RSS

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

    807607
      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.