Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: use of regular expressions in string.replaceAll

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default use of regular expressions in string.replaceAll

    I have a String in which I'm trying to replace all individual characters (except for spaces) surrounded by spaces with hyphens.
    I thought the code to do this would look something like this:
    str = str.replaceAll(" ^\\s ", " - ");

    However, this doesn't work, so if someone has a solution, it'd be much appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: use of regular expressions in string.replaceAll

    Try using negation of a character class...' [^\\s] '

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: use of regular expressions in string.replaceAll

    Could you please provide the exact line of code you are thinking? I don't know if those single quotes are to be in the code, or where I'm supposed to use them. Thanks.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: use of regular expressions in string.replaceAll

    I was single quoting the regular expression :
    str = str.replaceAll(" [^\\s] ", " - ");

  5. The Following User Says Thank You to copeg For This Useful Post:

    nesthead98 (June 17th, 2012)

Similar Threads

  1. Text Processing with Regular Expressions explained in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: February 8th, 2022, 05:16 PM
  2. Web Crawling & Java Regular Expressions
    By Nytol in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2011, 02:34 PM
  3. regular expressions
    By brad35309 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2010, 08:30 PM
  4. Text Processing with Regular Expressions explained in Java
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: August 6th, 2008, 02:03 AM
  5. Java program to validate an email address using Regular Expressions
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:26 AM

Tags for this Thread