Click here to Skip to main content
Click here to Skip to main content

Normal Casing of an Upper Case Paragraph Using .NET Regular Expressions

, 13 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Illustrates a method of taking a multi-sentence string that is in all caps, and converting to to normal case.

Introduction

Use regular expression engine in .NET to convert a multi-sentence string to normal case.

Background

Many developers use data from legacy systems that generate strings that are all upper case. Sometimes you may need an efficient way to display those lengthy strings in correct case where the first letter of every sentence is capitalized and the rest is lower case.

Using the code

The code is simple and does work but its not perfect. Proper nouns such as names of places and people will not be correct cased, and I dont know of a way to recognize them all in code. But this methodology seems to work in cases where proper nouns are not an issue.

The example is a console application in VB.NET. It's fairly well documented and I think most developers will not have a problem using it. I'd welcome any help to improve the regular expression pattern and if anybody knows of a way to recognize proper nouns in code - I'd love to see it.

Imports System.Text.RegularExpressions

 

Module Module1

Sub Main()

    'This is a way you can take an upper case sentence 
    'and convert it to proper case. This example uses
    'the .NET Regular Expressions engine for efficient
    'text pattern searching. It then uses a delegate method that is
    'called by the RegEx engine everytime it finds a match. The delegate
    'method merely uppercases the matched text.

    'UPPER CASE MESSAGE FROM A LEGACY SYSTEM
    Dim ORGmSG As String = "I HOPE THIS WORKS FOR ALL SITUATIONS. " & _
    "BUT ITS LIKELY THAT YOU MAY FIND A BUG. IF YOU DO THEN LET ME KNOW. " & _
    vbCrLf & vbCrLf & "NOW IS THE TIME, FOR ALL GOOD MEN, TO COME TO THE AID " & _
    "OF THEIR COUNTRY."

    'create an immutable regular expression object with its pattern set
    Dim reg As New Regex("^[a-z]|\b\. [a-z0-9]?| i ", RegexOptions.Multiline)

    'convert the upper case string to all lower, ask regex engine to
    'do a replace on it with the delegate method I called eval().
    Console.WriteLine(reg.Replace(ORGmSG.ToLower, AddressOf eval))

    Console.ReadKey()
End Sub


Private Function eval(ByVal m As Match) As String
  Return m.ToString.ToUpper()
End Function

End Module

History

3-12-2007 Initial code review.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Steve Killick
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberHeaven20203-Nov-10 8:51 
Generalnormal casing PinmemberBud Pass13-Mar-07 17:37 
GeneralRe: normal casing Pinmembermav.northwind13-Mar-07 20:12 
GeneralRe: normal casing PinmemberBud Pass14-Mar-07 16:18 
GeneralRe: normal casing Pinmembermav.northwind14-Mar-07 19:57 
GeneralRe: normal casing PinmemberBud Pass15-Mar-07 3:40 
AnswerRe: normal casing PinmemberSteve Killick15-Mar-07 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 13 Mar 2007
Article Copyright 2007 by Steve Killick
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid