Click here to Skip to main content
15,881,967 members
Articles / Mobile Apps
Article

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

Rate me:
Please Sign up or sign in to vote.
2.23/5 (5 votes)
13 Mar 2007 44.9K   177   13   7
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.

VB.NET
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Global Analyser3-Nov-10 8:51
Global Analyser3-Nov-10 8:51 
Generalnormal casing Pin
Bud Pass13-Mar-07 17:37
Bud Pass13-Mar-07 17:37 
GeneralRe: normal casing Pin
mav.northwind13-Mar-07 20:12
mav.northwind13-Mar-07 20:12 
GeneralRe: normal casing Pin
Bud Pass14-Mar-07 16:18
Bud Pass14-Mar-07 16:18 
GeneralRe: normal casing Pin
mav.northwind14-Mar-07 19:57
mav.northwind14-Mar-07 19:57 
GeneralRe: normal casing Pin
Bud Pass15-Mar-07 3:40
Bud Pass15-Mar-07 3:40 
AnswerRe: normal casing Pin
Steve Killick15-Mar-07 4:32
Steve Killick15-Mar-07 4:32 

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

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