Writing a regex for IntiCap of a String?

Posted on 2005-05-10
Last Modified: 2007-12-19
I have to build a utility where I want to do a InitCap on any arbitrary String the user passes.

I looked up all the solution on google seem to be doing it with loops etc. I want to use regex.

Nee help with this .. Example String is "    hello world 12ab AAA   ".

I want to preserve the leading and trailing spaces.

Thanx again.

Question by:rahulkothari
    LVL 15

    Expert Comment

    >> I want to use regex.
    Java standard regexp don't have (like perl) the ability to use something similar to \U or \C to upper case what after.
    If you use Jakarta ORO which supports a more like perl regexp then you can do that using the above.
    LVL 15

    Expert Comment

    If you are ok with downloading and using Jakarta ORO ( then the syntax should be similar to this perl regexp:

    $a="    hello world 12ab AAA   ";
    print $a;

    result:     Hello World 12ab AAA
    LVL 86

    Expert Comment

    You could do this with java.util.regex package:

                      String s = "    hello world 12ab AAA   ";
                      Pattern p = Pattern.compile("\\s*?(\\w)");
                      Matcher m = p.matcher(s);
                      if (m.find()) {
                            int end = m.end();
                            StringBuffer sb = new StringBuffer(s);
                            sb.setCharAt(end - 1, Character.toUpperCase(s.charAt(end - 1)));
    LVL 15

    Accepted Solution

    Not sure how the above will do the job.
    If you didn't expect for one regular expression that will do the job (as I mentioned above) and you are ok with code then you can use this code:

    String s = "    hello world 12ab AAA   ";
    Pattern p = Pattern.compile("(\\s*)([^\\s]+)");
    Matcher m = p.matcher(s);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
          String replaceWith = + Character.toUpperCase( +;
                     m.appendReplacement(sb, replaceWith);

    System.out.println(":" + sb + ":");

    java Main
    :    Hello World 12ab AAA   :

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now