7
votes

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

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

9
You should either specify a programming language, or ask for a language agnostic answer.Brad Gilbert
@Jeremy: I added the VB.NET equivalent to my responseAhmad Mageed

9 Answers

29
votes

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!");
11
votes

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.


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.

6
votes
Dim s As String = "your string"
Dim s2 As String = StrConv(s, VbStrConv.ProperCase
MessageBox.Show(s2)
5
votes

.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);
    }
}
4
votes
 s/\b(\w+)\b/ucfirst($1)/ge
2
votes

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()

2
votes

Use ProperCase function:

Dim Str As String = "the quick brown fox jumps over the lazy dog"
Dim NewStr As String = StrConv(Str, VbStrConv.ProperCase) 

          

0
votes

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

0
votes

Do this on key press event of your text box.

If e.KeyChar <> ChrW(Keys.Back) Then
            If txtEname.Text = "" Then
                e.KeyChar = UCase(e.KeyChar)
            End If


        End If