my dog learned polymorphism*
The moose likes Java in General and the fly likes Regex for Init Cap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex for Init Cap" Watch "Regex for Init Cap" New topic
Author

Regex for Init Cap

Jennifer Moran
Ranch Hand

Joined: Oct 21, 2002
Posts: 60
Hi -

I wanted to Capitalize all first letters of a name (whether it's a proper name or company name etc). I was thinking instead of writing a function I could do it with a regexp but I am not that 'good' with regexp and was looking for advice. Should I use regexp or just code a function? If I use regexp could someone provide me with the details?

thanks,

Jennifer
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

If I use regexp could someone provide me with the details?


Regex is not a simple library -- it is argubly its own language. So... if you want to use regex, you need to just dive in and learn it. (Basically, get a book and learn it. It won't be wasted, as once you get the regex hammer into your toolbox, everything will start looking like a nail... )

Once you get started... take a look at the find(), appendReplacement(), and appendTail() methods. Basically, you need the regex to look for the beginning of the word that starts with a lower case character, and then convert them to upper case.

The standard regex replaceFirst() and replaceAll() methods don't support the conversion to uppercase (or any type of conversion), so you'll need to use the appendXXX() methods.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jennifer Moran
Ranch Hand

Joined: Oct 21, 2002
Posts: 60
Thanks so much for the point in the right direction.

Much appreciated,

Jennifer
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Jennifer,

Mastering Regular Expressions, 3ed is a very good book on the subject. It has good coverage of several languages' regex libraries, including Java.

John.

Edit: fixed link
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
John de Michele wrote:Jennifer,

Mastering Regular Expressions, 3ed . . .
Thank you, but there is an error in that link. This is a link to that same book in our book reviews. It was given 10 horseshoes. There is a link to Amazon from the review to purchase the book.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Campbell Ritchie wrote:It was given 10 horseshoes.

Oh great, another book I have to buy now! And I still have to finish two other books!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jennifer Moran
Ranch Hand

Joined: Oct 21, 2002
Posts: 60
I'll definitely check it out.

Thanks again!!

Jennifer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex for Init Cap
 
Similar Threads
backslash is removed from the parameter when javascript function get the parameter from java call
Javascript noob question
Need help in regular expression
regular expressions
un-format a phone number