Results 1 to 5 of 5
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default Using Regex for Capitalization

    Java Code:
    public class Grammar {
    	
    	private static String capitalize(String str) {
    		return str.toLowerCase().replaceAll("(^|\\s)([a-z])","$1$2".toUpperCase());
    		
    	}
    	
    	public static void main(String[] args) {
    		System.out.println(capitalize("hi dude"));
    	}
    }
    what's wrong with this? It won't capitalize the first letter of every word. Any help appreciated.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It calls toUpperCase on the String literal "$1$2", which does nothing, and then uses that result as the replacement String.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    is it possible for me to evaluate $1$2 first?

    p.s: I need to do this without using loops, recursive, or anything that is not regex.
    Last edited by Lil_Aziz1; 02-14-2010 at 10:38 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    is it possible for me to evaluate $1$2 first?
    I'm not familiar with a way to do this (but this doesn't mean it doesn't exist).

    p.s: I need to do this without using loops, recursive, or anything that is not regex.
    Why?

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    :( I can't find anything on google either. I need to do it using regex because it's a challenge from a friend.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. Interpretation of regex?
    By Ms.Ranjan in forum New To Java
    Replies: 2
    Last Post: 04-23-2009, 06:37 PM
  2. Multiline Regex
    By zriggle in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 04:49 AM
  3. Some help with regex and loop
    By moaxjlou in forum New To Java
    Replies: 21
    Last Post: 11-02-2008, 10:24 PM
  4. [SOLVED] More RegEx help
    By JT4NK3D in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 04:07 AM
  5. Regex Quantifiers Example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 10:44 AM

Posting Permissions

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