Solved

regular expression back references?

Posted on 2002-04-04
4
252 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 65
iterator/ListIterator approach 17 54
null output 3 43
learn programming 8 72
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 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