Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

regular expression back references?

Posted on 2002-04-04
4
Medium Priority
?
271 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

609 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