tags:

views:

1067

answers:

8

Is it possible to uppercase the first character of each word using regex?

I'm going to be using this in VB.net (SSIS)

A: 

You could do that, however this is a pretty common function in most programming languages. For example, the function is ucwords($word) in PHP.

Kyle J. Dye
I've never used PHP, but don't recall this method in other languages I've used, although I could have just not noticed it.
Jeremy
+1  A: 

Take a look at Capitalize First Letter of Each Word and at C# Uppercase Words in String, I posted in two languages, Perl and C#, because you haven't specified the language that you're using.

Nathan Campos
+4  A: 
 s/\b(\w+)\b/ucfirst($1)/ge
Chris Ballance
Nice and easy -- `s/\b(\w+)/\u$1/g` or `s/\b(\w)/\U\1/g` for short. Unfortunately, the question now specifies VB.NET, so this Perl answer is no longer relevant...
ephemient
+2  A: 

Not in "pure" regex, but most platform-specific implementations have a way to do it:

For example, in python:

import re
re.compile(r'\b\w').sub(lambda x: x.group(0).upper(), 'hello')

In this case we pass a callable lambda to the sub() method (rather than a replacement string) that will return the matching string upper cased. Most languages have some equivalent where you pass a callable as the 'replacement'.

In VB.NET you can pass your 'replacement' lambda as Function (x) x.Value(0).ToString().ToUpper()

ʞɔıu
A: 

No , but you could user a simple script, it depends on what language are you using (or is it shell?)

Lex
I need to do this in SSIS, which uses VB.net
Jeremy
+2  A: 

EDIT: VB.NET code added below

Dim input As String = "The quick brown fox jumps over the lazy dog"
Dim pattern As String = "\b(\w|['-])+\b"
' With lambda support:
Dim result As String = Regex.Replace(input, pattern, _
    Function (m) m.Value(0).ToString().ToUpper() & m.Value.Substring(1))

If you can't use lambdas then use a MatchEvaluator instead:

Dim evaluator As MatchEvaluator = AddressOf TitleCase
Dim result As String = Regex.Replace(input, pattern, evaluator)

Public Function TitleCase(ByVal m As Match) As String
    Return m.Value(0).ToString().ToUpper() & m.Value.Substring(1)
End Function

It's not really Title case in the sense of the MS Word formatting, but close enough.


You didn't specify the language, but in C# you could do this:

string input = "The quick brown fox jumps over the lazy dog";
string pattern = @"\b(\w|['-])+\b";
string result = Regex.Replace(input, pattern,
                    m => m.Value[0].ToString().ToUpper() + m.Value.Substring(1));

This nicely handles one letter words, as Substring won't throw an error on something such as "A" in the input.

Ahmad Mageed
+3  A: 

.NET has builtin support for this. See TextInfo.ToTitleCase for documentation.

My code contains some extension methods for C#. I assume VB.NET has those too, but I do not know VB.NET well enough to convert them myself.

public static class StringExtensions {
    public static string ToTitleCase(this string value) {
        return value.ToTitleCase(CultureInfo.InvariantCulture);
    }

    public static string ToTitleCase(this string value, CultureInfo culture) {
        return value.ToTitleCase(culture.TextInfo);
    }

    public static string ToTitleCase(this string value, TextInfo textInfo) {
        return textInfo.ToTitleCase(value);
    }
}
Simon Svensson
+2  A: 

Why not just use the inbuilt TextInfo.ToTitleCase() method already in the .NET Framework?

string capitalized = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase("this string should be capitalized!");
Dan Diplo
Didn't know about this. Could be a great solution for me. Thanks!
Jeremy