Special chars in regular expressions - Problems

  • Follow


    I am trying to match (for removal) certain characters from a file,using regular expressions (java.util.regex.*).  However, the Java 1.6compiler does not like any of the characters that are preceded by thebackslash ("/"), although the Java SE 6.0 API documentation forjava.util.regexClass Pattern says it`s OK.  The code may be found below.    For the line of codeString patternStr = "[\d\p{Space}\p{Punct}\p{Blank}]+";it says that each of the characters after the slashes (d and p) is an"illegal escape character."  I understood the brackets [...] to meanmatch any of the characters enclosed by them.    Is it obvious to anyone what I am doing wrong?       Thanks, Alanimport java.net.*;import java.io.*;import java.util.regex.*;public class CleanTokens{   public static void main ( String[] args ) throws IOException   {      try      {         BufferedReader infile = new BufferedReader(newFileReader("input.txt"));         PrintWriter outfile = new PrintWriter(newFileOutputStream("output.txt"));	 String inputStr = "";	 // Define regular expression pattern	 String patternStr = "[\d\p{Space}\p{Punct}\p{Blank}]+";	 // Compile regular expression	 Pattern pattern = Pattern.compile(patternStr);	 // Define matcher object 	 Matcher matcher = pattern.matcher(str);	 // Define replacement string (nothing --- will delete pattern)	 String replacementStr = "";         while ((inputStr = infile.readLine()) != null)         {	    // Take the current string	    matcher.reset(inputStr);	    // Remove the characters that match the pattern	    inputStr = matcher.replaceAll(replacementStr);	    // Write out the modified string            outfile.println(str);         }         infile.close();         outfile.close();      }      catch (IOException e) {e.printStackTrace();}   }}
0
Reply jalanthomas (125) 10/6/2007 1:32:20 AM

 Never mind!  I had to double the backslashes ("\\") in the expressionas follows.  AlanString patternStr = "[\\d\\p{Space}\\p{Punct}\\p{Blank}]+";
0
Reply Alan 10/6/2007 1:42:07 AM


1 Replies
147 Views

(page loaded in 0.496 seconds)

Similiar Articles:













7/28/2012 2:33:24 AM


Reply: