Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split PascalCase string except for acronyms

I have a list of words that need to be made human readable, such as FirstName to First Name, LastName to Last Name, and in some cases, acronyms like ARBs to remain as is. The latter was recently introduced and has caused a display issue since our regular expression returns AR Bs. Here's what we have, which I know is insufficient for acronyms:

([A-Z][a-z]+)

I've found other expressions on SO and on other sites that are able to work with acronyms, however they work on strings where the acronym is within the string rather than being the entire string. I can do simple regular expressions, but this is too tricky for my skills. I would provide other examples for testing if I had them, but all of the strings work fine except the new one, ARBs. Thank you.

Update: Here's the code usage

string friendlyName = Regex.Replace(field.Name, "([A-Z][a-z]+)", " $1", RegexOptions.Compiled).Trim();
like image 912
Mathachew Avatar asked Dec 13 '11 20:12

Mathachew


People also ask

Why do I need to convert string to lower case before totitlecase?

This is my take on it, the akward part is that you need to convert the string to lower case before calling ToTitleCase because if all letters in a word are uppercase it will remain uppercased as it is identified as an acronym and preserved by the ToTitleCase implementation. Show activity on this post.

How to convert text string to proper case with exceptions in Excel?

How to convert text string to proper case with exceptions in Excel? In Excel, you can apply the Proper function to convert text strings to proper case easily, but, sometimes, you need to exclude some specific words when converting the text strings to the proper case as following screenshot shown.

How to prevent string objects from being created from multiple calls?

Use instead multiple calls to prevent unneccessary creation of string objects. There are overloads for the different types so you could call it once for the starting uppercase char and another which takes the remaining string.

How do you split a string into 3 parts?

Split numeric, alphabetic and special symbols from a String. Given a string str, divide the string into three parts one containing a numeric part, one containing alphabetic and one containing special characters.


1 Answers

Wouldn't [A-Z]+[a-z]* do it? That should match one or more upper-case letters followed by zero or more lower-case letters. So ARBs would remain a single entity, but CamelCase would be split into Camel Case.

like image 77
Jim Mischel Avatar answered Sep 18 '22 22:09

Jim Mischel