Home > Back-end >  Regex to change Uppercase to Propercase inside strings, but only the full uppercase strings
Regex to change Uppercase to Propercase inside strings, but only the full uppercase strings

Time:11-04

Example string:

This is an EXAMPLE SENTENCE, just do give YOU AN Example on Monday, BUT NOT FOR Friday.

Expected output:

This is an Example Sentence, just do give You An Example on Monday, But Not For Friday.

I've tried Propper, but this changes every first letter from every word to uppercase.

CodePudding user response:

I don't think it's possible to change the case using Regular Expressions, here's what you can try instead:

=ARRAYFORMULA(LAMBDA(word,JOIN(" ",IF(EXACT(UPPER(word),word),PROPER(word),word)))(SPLIT(A1," ")))

This formula is SPLITting the sentence by the space " " to separate each word and uses IF to check if each word converted to UPPERcase is EXACTly the same as the word not converted, if it is, it replaces it with PROPER casing, otherwise it keeps it as it is. Finally, it JOINs the words back together.

CodePudding user response:

Arrayformula

You need to specifiy your range once in a Byrow() range, in this case A2:A

=ArrayFormula(
 BYROW(A2:A, LAMBDA(x, IF(x="",,
 LAMBDA(r, TEXTJOIN(" ",1,MAP(r,PROPER(r), LAMBDA(i,a, 
           IF(REGEXMATCH(i, "\b[A-Z] \b")*1<>1,i,PROPER(a))))))
       (SPLIT(x," "))))))

enter image description here

One Cell

=ArrayFormula(
 LAMBDA(r, TEXTJOIN(" ",1,MAP(r,PROPER(r), LAMBDA(i,a, 
           IF(REGEXMATCH(i, "\b[A-Z] \b")*1<>1,i,PROPER(a))))))
       (SPLIT(A2," ")))

enter image description here

Used formulas help
ARRAYFORMULA - BYROW - LAMBDA - IF - TEXTJOIN - MAP - PROPER - REGEXMATCH - SPLIT

  • Related