Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Special chars in regular expressions - Problems

Thread view: 
Alan - 06 Oct 2007 02:32 GMT
I am trying to match (for removal) certain characters from a file,
using regular expressions (java.util.regex.*).  However, the Java 1.6
compiler does not like any of the characters that are preceded by the
backslash ("/"), although the Java SE 6.0 API documentation for
java.util.regex
Class Pattern says it`s OK.  The code may be found below.

   For the line of code

String 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 mean
match any of the characters enclosed by them.

   Is it obvious to anyone what I am doing wrong?       Thanks, Alan

import 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(new
FileReader("input.txt"));

        PrintWriter outfile = new PrintWriter(new
FileOutputStream("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();}
  }
}
Alan - 06 Oct 2007 02:42 GMT
Never mind!  I had to double the backslashes ("\\") in the expression
as follows.  Alan

String patternStr = "[\\d\\p{Space}\\p{Punct}\\p{Blank}]+";


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.