Solved

regular expression back references?

Posted on 2002-04-04
4
226 Views
Last Modified: 2010-03-31
stupid question - I am using String's replaceAll(pattern, replacement) how do I reference the captured groups "(whatever)" from the pattern in the replacement?  \1 doesn't work (and the documentation says that's what it is) and neither does \\1

thanks
0
Comment
Question by:_D_
  • 2
4 Comments
 

Author Comment

by:_D_
ID: 6920218
ok never mind that - figured it out, different question though:

what I need to do is convert the string to "Title Case", ie first letter of every word capitalized, what I have for the replace so far is:

replaceAll("\\b(\\w)","$1")

which just relaces each first character with the same one - how to I capitalize that back reference?  Or do I need to do this is some completely different way?

thanks
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6920351
.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 6922112
The most straightforward way -- without using regular expressions -- is probably something like this:

    public String toTitleCase(String s) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        boolean fixNext = true;
        for (int i = 0; i < length; ++i) {
            if (Character.isWhitespace(chars[i])) {
                fixNext = true;
            } else if (fixNext) {
                chars[i] = Character.toTitleCase(chars[i]);
                fixNext = false;
            }
        }
        return new String(chars);
    }

Best regards,
Jim Cakalic
0
 

Author Comment

by:_D_
ID: 6933488
no quite what I was looking for, but I don't see a better way
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

914 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