Results 1 to 3 of 3
  1. #1
    Daedalus is offline Member
    Join Date
    Sep 2008
    Rep Power

    Question remove all punctuation except dots hyphens and spaces with a regular expression


    I would like to remove all punctuation from an article that is a string but retain the spaces, hyphens and dots so I can still determine word and sentence boundarys.

    I have tried

    txt = txt.replaceAll("\\W([^\\.]|[^\\s]|[^\\-])", "");

    however its matching a non word character followed by some thing that's not a dot or space or hyphen rather than a non word character except a dot or space or hyphen.

    How do you do exceptions?, are they supported? If not how would I go about solving this problem with out specifying every single possible punctuation character (including non ascii characters)?

    Thanks in advance.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    Well, it's doing exactly what your regex says "a non word character followed by (not a '.' or not a space or not a hyphen"). I think what you want is "not a word character and not a dot and not a space and not a hyphen", which is the same as "not [ a word character or a dot or a space or a hyphen], which would be...


    ...hope I got it right. ;p

  3. #3
    Daedalus is offline Member
    Join Date
    Sep 2008
    Rep Power


    yes it was almost right except that the "^" character at the beginning has a different meaning when it is outside the brackets (matches at start of word or something I think) if you put it inside the brackets it works fine>


    Thanks for the help
    Last edited by Daedalus; 01-05-2011 at 07:19 PM.

Similar Threads

  1. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 10:01 PM
  2. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 12:15 PM
  3. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 04:55 AM
  4. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM
  5. Regular expression with Intersections
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 01:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts