Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular expression help

Posted on 2009-07-09
14
Medium Priority
?
201 Views
Last Modified: 2013-11-07
Hello

Given a string that is in capitals, how could i convert the string so that only the first letter of every word was a capital and the rest was lower case.

There is an added complication that a have a list of certain words which would need to be made all lower case: on, upon, in

Also any words that contain a number must remain in capitals. This last one might seem odd but the input string in question is an address. Sometimes the county where someone lives is not available and is given as an abbreviated postcode.

MAny thanks
0
Comment
Question by:andieje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
14 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 25042238
This is not done by regular expression. Regular expressions are used to match strings with patterns not to modify/process strings. You need to break up the address into individual words and then perform the manipulation. Let me know if you are still interested and i would try to produce some code.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 25043683
A hybrid method using regex and StrConv (from the VisualBasic namespace)
Imports System.Text.RegularExpressions
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Debug.WriteLine(FormatCase("THIS IS MY STRING IN UPON BUT NOT UON CAPS."))
    End Sub
 
    Private Function FormatCase(ByVal Value As String) As String
        Dim Ret As String = StrConv(Value, VbStrConv.ProperCase)
        'Convert necessary words to lower case
        Ret = Regex.Replace(Ret, "\x20In|Upon|On\x20", AddressOf Eval)
        Return Ret
    End Function
 
    Private Function Eval(ByVal m As Match) As String
        Return m.Value.ToLower
    End Function
 
End Class

Open in new window

0
 
LVL 38

Expert Comment

by:PaulHews
ID: 25043767
Nevermind there's some glaring problems with that.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 25043888
Better version:
Imports System.Text.RegularExpressions
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Debug.WriteLine(FormatCase("3THIS IS MY 3STRING IN UPON INNIS BUT NOT UON CAPS IN."))
    End Sub
 
    Private Function FormatCase(ByVal Value As String) As String
        Dim Ret As String = StrConv(Value, VbStrConv.ProperCase)
        Debug.WriteLine(Ret)
        Ret = Regex.Replace(Ret, "\b\S*\d+\S*\b", AddressOf NumFix)
        'Convert necessary words to lower case
        Ret = Regex.Replace(Ret, "\b(In|Upon|On)\b", AddressOf Eval)
        Return Ret
    End Function
 
    Private Function Eval(ByVal m As Match) As String
        Return m.Value.ToLower
    End Function
    Private Function NumFix(ByVal m As Match) As String
        Return m.Value.ToUpper
    End Function
 
End Class

Open in new window

0
 

Author Comment

by:andieje
ID: 25249714
sorry for delay, i've been away. I will look at this question over the next day or so and close it. I already have a terrible work backlog i have to get though.

thanks for your patience
0
 

Author Comment

by:andieje
ID: 25249724
Please can i keep this open. I've been away but no i'm back. I have some things to catch up on but will give this my attention as soon as i can.
0
 

Author Comment

by:andieje
ID: 25260756
Many thanks, will look at it over the weekend
0
 

Author Comment

by:andieje
ID: 25268342
Hi Paul

That function looks great. What is that version of replace where you pass in the address of a function. I've never seen that before.

thanks
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 25270365
It's the Replace overload that takes a MatchEvaluator delegate.  The function must take a match parameter and return a string to fit the signature of the MatchEvaluator delegate.

Regex.Replace Method (String, String, MatchEvaluator)
http://msdn.microsoft.com/en-us/library/ht1sxswy.aspx

It gives you a lot more flexibility in the replacement of elements than a straight string replace.

0
 

Author Closing Comment

by:andieje
ID: 31601510
many thanks
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question