Regex – Conditionally Modifying User Data to “Title Case” for Phone Labels

Sadly a lot of UC is well, about those pesky end users.  This generally involves user data of some sort, and inevitably this is bad data.  Formatting text to get your 1k users can be a pain – even when the AD team you are working with are on top of things.

As an example, something I see a lot is changing data looking like this:

1234-AS JONES

To this:

AS Jones – x1234

This requires a conditional match, moving things around, and a case change (Title Case).  The first 2 letters are initials, so I need to account for those as they must stay capitalized.

Excel requires a number of steps to do this.  I need to use a text editor here or a CLI tool like SED.  For me, Notepad++ is my poison of choice as I’m still bound to Windows.  Its a good tool use.

Firstly, I could try and use it’s built-in features available:

propercase

 

But this doesn’t give me the CONDITIONAL control that I want.  Using Excel is even worse.  I hate it and try avoid it at all costs.   I need REGEX to complete this task.


 

My requirement is completed using tagged expressions and the following:

  • \L – Translates everything to lowercase until the end of the replacement string
  • \u – Translates the next letter to uppercase

I’m not going to go into detail on the rest, as if you’re a voice engineer on Cisco – you know the rest already (I hope!).


 

So let’s look at his this is achieved in Notepad++:

  • Input: 1234-AS JONES
  • Regular Expression: ^([0-9]{4})-([A-Z]+) ([A-Za-z]+)
  • Replaced with: \2 \L\u\3 – x\1

The output is now exactly what I want:

AS Jones – x1234

In NPP the regex input looks like this:

regex

 

 

What is neat about this is that I wanted to conditionally only change the case of the surname – this left the user’s initials intact and did not give me camel case which I wanted to avoid.

Please see here for more info.

Hope this helps!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.