Question Regex replace to modify trailing character

DougInCanada

New member
Joined
Feb 14, 2013
Messages
2
Programming Experience
Beginner
I'm trying to figure out how to change a trailing character using Regex Replace.

I have a text field for last name that I'm trying to standardize using Regex.

I'd like to change the trailing character from lowercase to uppercase every time an apostrophe occurs (ie: O'connor should be O'Connor) , a hyphen occurs (ie: Billy-bob should be Billy-Bob) or "Mac" or "Mc" occurs (ie: Macdonald should be MacDonald and Mcdougall should be McDougall).

Can anyone assist me?

Much appreciated,

Doug
 
Hi,

I would think that RegEx is a bit much for this solution since this should just a bit of string manipulation. In this example I use the Validated event of the TextBox to check for the existence of your search character and then manipulate your string, if needed, to convert the trailing character of your search string to an Upper Case character:-

VB.NET:
Private Sub TextBox1_Validated(sender As Object, e As System.EventArgs) Handles TextBox1.Validated
  'Setup the search value
  Dim strSearchValue As String = String.Empty
  If TextBox1.Text.Contains("'") Then
    strSearchValue = "'"
  ElseIf TextBox1.Text.Contains("-") Then
    strSearchValue = "-"
  ElseIf TextBox1.Text.Contains("Mac") Then
    strSearchValue = "Mac"
  ElseIf TextBox1.Text.Contains("Mc") Then
    strSearchValue = "Mc"
  End If
 
  'If a search value exists then use string manipulation to
  'Update the string as needed
  If Not strSearchValue = String.Empty Then
    TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.IndexOf(strSearchValue) + strSearchValue.Length) & TextBox1.Text.Substring(TextBox1.Text.IndexOf(strSearchValue) + strSearchValue.Length, 1).ToUpper & _
    TextBox1.Text.Substring(TextBox1.Text.IndexOf(strSearchValue) + (strSearchValue.Length + 1), (TextBox1.Text.Length - (TextBox1.Text.IndexOf(strSearchValue) + (strSearchValue.Length + 1))))
  End If
End Sub

Hope that helps.

Cheers,

Ian
 
        Dim s = "O'connor Billy-bob Macdonald Mcdougall"
        s = Regex.Replace(s, "(?<=['-]|Mc|Mac)\w", Function(m) m.Value.ToUpper)

I think this will only work with VB 2008 and newer, otherwise use AddressOf for MatchEvaluator delegate function.
 
Hi JohnH,

You still keep throwing me with those RegEx expressions. :highly_amused:

Can I confirm what you have said in this expression "(?<=['-]|Mc|Mac)\w". Are you basically saying:-

1) LOOK AROUND, BUT DO NOT SELECT, Apostrophie, Hyphen, Mc or Mac using this bit "(?<=['-]|Mc|Mac)"
2) Then, once there, Select the next character using "\w"

Is that it?

Many thanks and cheers.

Ian
 
Hi JohnH,

Many thanks for that, and Yes, I have also been using that same resource for RegEx. Just still getting used to some of the more advanced options available.

Cheers and have a good day.

Ian
 
Ian,

Thanks for the alternative, it works great. I just had to break out each ElseIf statement, since it's possible to have a ' and a - in the same name (it's actually happened...).

Thought I really should continue to try to get my head around regex. Thanks JohnH for some explanatory of 'The Da Vinci Code'...I mean regex and yes that site is really good, learned alot about regex in vbscript, now I need to hit the learning curve in VB 2010...
 
Back
Top