Split PascalCase String Except Abbreviations

I have a list of words that need to be made human readable, such as FirstName to Name Name, LastName to Last Name, and in some cases, abbreviations like ARB remain as they are. The latter was recently introduced and caused a mapping problem as our regular expression returns AR Bs . Here, what we have, that I know, is not enough for abbreviations:

([AZ][az]+) 

I found other expressions on SO and on other sites that can work with abbreviations, however they work with strings where the abbreviation is inside the string, and not as a whole string. I can do simple regular expressions, but it's too complicated for my skills. I would provide other examples for testing if I had them, but all the lines work fine except for the new ARB. Thanks.

Update: code is used here

 string friendlyName = Regex.Replace(field.Name, "([AZ][az]+)", " $1", RegexOptions.Compiled).Trim(); 
+7
source share
3 answers

Wouldn't [AZ]+[az]* do this? This must match one or more uppercase letters followed by zero or more lowercase letters. Thus, ARBs will remain the only object, but CamelCase will be split into a Camel Case .

+12
source

How about this?

 [AZ][az]+|[AZ] 
0
source

A line / paragraph / sentence, including Acronyms, can be converted to human readable sentences / line. I was just trying to format the Pascal Cased string, I was researching more and even trying to convert Acronyms to a clear format.

Test data:

Input: "QWERTYSomeThing OmitTRYSomeThing MayBeWorkingFYI"

Exit: "QWERTY Some Thing Omit TRY Some Thing May Be Working FYI"

Code: Pass the input string to the method below.

  private static string FormatPascalAndAcronym(string input) { var builder = new StringBuilder(input[0].ToString()); if (builder.Length > 0) { for (var index = 1; index < input.Length; index++) { char prevChar = input[index - 1]; char nextChar = index + 1 < input.Length ? input[index + 1] : '\0'; bool isNextLower = Char.IsLower(nextChar); bool isNextUpper = Char.IsUpper(nextChar); bool isPresentUpper = Char.IsUpper(input[index]); bool isPrevLower = Char.IsLower(prevChar); bool isPrevUpper = Char.IsUpper(prevChar); if(!string.IsNullOrWhiteSpace(prevChar.ToString()) && ((isPrevUpper&& isPresentUpper && isNextLower) || (isPrevLower&&isPresentUpper&&isNextLower)|| (isPrevLower&&isPresentUpper&&isNextUpper))) { builder.Append(' '); builder.Append(input[index]); } else{ builder.Append(input[index]); } } } return builder.ToString(); } 
0
source

All Articles