Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex for Init Cap

 
Jennifer Moran
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jennifer Moran
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the point in the right direction.

Much appreciated,

Jennifer
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jennifer Moran
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll definitely check it out.

Thanks again!!

Jennifer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic