Click here to Skip to main content
Licence 
First Posted 13 Mar 2007
Views 21,674
Downloads 85
Bookmarked 12 times

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

By | 13 Mar 2007 | Article
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

About the Author

Steve Killick

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberHeaven20208:51 3 Nov '10  
Generalnormal casing PinmemberBud Pass17:37 13 Mar '07  
GeneralRe: normal casing Pinmembermav.northwind20:12 13 Mar '07  
GeneralRe: normal casing PinmemberBud Pass16:18 14 Mar '07  
GeneralRe: normal casing Pinmembermav.northwind19:57 14 Mar '07  
GeneralRe: normal casing PinmemberBud Pass3:40 15 Mar '07  
AnswerRe: normal casing PinmemberSteve Killick4:32 15 Mar '07  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 13 Mar 2007
Article Copyright 2007 by Steve Killick
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid