Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active November 28, 2022 17:30
Show Gist options
  • Select an option

  • Save ninmonkey/18e1820b580b50b8c201fac50507f85c to your computer and use it in GitHub Desktop.

Select an option

Save ninmonkey/18e1820b580b50b8c201fac50507f85c to your computer and use it in GitHub Desktop.
VS Code regex replace changes capitalization cases

Capitalizes the first character, lowercases the rest

Regex used was a simple/flag Or list,

Regex: (count|SUBSET|Hold|Then|Else|end|subset|Concat|if|\beq\b|int|trim)
Replacement: \u\L$1

docs

This is done with the modifiers \u\U\l\L, where \u and \l will upper/lowercase a single character, and \U and \L will upper/lowercase the rest of the matching group.

The modifiers can also be stacked - for example, \u\u\u$1 will uppercase the first three characters of the group, or \l\U$1 will lowercase the first character, and uppercase the rest. The capture group is referenced by $n in the replacement string, where n is the order of the capture group

If you use short segments first, you can use sub-word capitalization

For example: Compare the final capitalization

regex  : (bat|man|batman)
pattern: batman
result : BatMan
regex  : (batman|bat|man)
pattern: batman
result : Batman

Sample input

HOLD16 ≔ COUNT:~,FIELD_FACTOR;
SUBSET:~,^,1,1;
SUBSET:^,^,1,1;
HOLD6 ≔ TRIMEND:^;
SUBSET:~,^,1,1;
SUBSET:^,^,2,1;
HOLD7 ≔ TRIMEND:^;

IF:HOLD16,GE,1,INT;
    THENSUBSET:~,^,1,2;
    SUBSET:^,^,3,1;
    HOLD8 ≔ TRIMEND:^;
    SUBSET:~,^,1,2;
    SUBSET:^,^,4,1;
    HOLD9 ≔ TRIMEND:^;
ENDIF;

IF:HOLD16,GE,2,INT;
    THENSUBSET:~,^,1,3;
    SUBSET:^,^,5,1;
    HOLD10 ≔ TRIMEND:^;
    SUBSET:~,^,1,3;
    SUBSET:^,^,6,1;
    HOLD11 ≔ TRIMEND:^;
ENDIF;

IF:HOLD16,GE,3,INT;
    THENSUBSET:~,^,1,4;
    SUBSET:^,^,7,1;
    HOLD12 ≔ TRIMEND:^;
    SUBSET:~,^,1,4;
    SUBSET:^,^,8,1;
    HOLD13 ≔ TRIMEND:^;
ENDIF;

IF:HOLD16,GE,1,INT;
    THENHOLD14 ≔ MATH:INT,HOLD7,/,HOLD9;
ENDIF;

IF:HOLD16,GE,2,INT;
    THENHOLD15 ≔ MATH:INT,HOLD9,/,HOLD11;
ENDIF;

INSFORMAT: ,0,0;
CONCAT:HOLD6,^;
IF:HOLD16,GE,1,INT;
    THENCONCAT:HOLD14;
    ELSECONCAT:HOLD7;
ENDIF;

IF:HOLD16,GE,1,INT;
    THENCONCAT:~,HOLD8,^;
ENDIF;

IF:HOLD16,EQ,1,INT;
    THENCONCAT:HOLD9;
ENDIF;

IF:HOLD16,GE,2,INT;
    THENCONCAT:HOLD15;
ENDIF;

IF:HOLD16,GE,2,INT;
    THENCONCAT:~,HOLD10,^,HOLD11;
ENDIF;

IF:HOLD16,GE,3,INT;
    THENCONCAT:~,HOLD12,^,HOLD13;
ENDIF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment