1

Update: has been implemented in VS Code now.


I've got this samplestring:

registerForm.main.myApplicant = value

I need to replace this with:

registerForm.main.getMyApplicant() = value

The regex I've come up with is:

\w+\.\w+\.(\w)(\w+)

Where $1 equals the m that needs to be transformed into uppercase. And $2 equals the rest of the word that needs to have () appended.

When I do:

FIND:    \w+\.\w+\.(\w)(\w+)
REPLACE: get\u$1$2()

It just prints out \u instead of making it uppercase. How can I perform regex on the capture group $1?

I've also tried $1=\u$1

Here's a regexr demo

Joel
  • 5,732
  • 4
  • 37
  • 65
  • In which regex flavor have you found the `\u`? – rioV8 Mar 06 '20 at 11:37
  • @rioV8 Not sure, I just searched how to replace regex-result with uppercase and found some threads that suggested that \u should work. Do you happen to know which flavor vs-code uses? Nvm, found it. seems to be using javascript flavor. – Joel Mar 06 '20 at 11:38
  • If you select `JavaScript` at regexr you can see the possibilities of the replace substitutions and `\u` is not one of them – rioV8 Mar 06 '20 at 11:43
  • found the flavor: TextMate Snippets, VSC Snippets use the TM format but not the `\u` syntax but a named transform like Mark used. https://macromates.com/manual/en/snippets – rioV8 Mar 09 '20 at 15:46

3 Answers3

1

A two step possibility

1)

FIND:    \w+\.\w+\.(\w)(\w+)
REPLACE: get@@@$1$2()

2)

  • select @@@
  • select all: Ctrl+Shift+L
  • expand selection 1 char: Ctrl+Shift+ArrowRight
  • use command: Transform to UpperCase
  • shrink selection 1 char: Ctrl+Shift+ArrowLeft
  • delete selection: Delete
  • stop multi cursor: Escape
rioV8
  • 24,506
  • 3
  • 32
  • 49
1

You may use a two regex step approach that can be used in global file search and replace.

I will modify the (\w) group pattern to ([a-z]) because we only need to match those strings that have a lowercase letter in that group.

Step 1:
Find What: \w+\.\w+\.([a-z])(\w+)
Replace With: get###ABCDEFGHIJKLMNOPQRSTUVWXYZ$1$2()

where ### is a fake anchor text that will help you identify the text you need to manipulate in the second step. The text will currently look like

get###ABCDEFGHIJKLMNOPQRSTUVWXYZmyApplicant() = value
get###ABCDEFGHIJKLMNOPQRSTUVWXYZnewValue() = value
get###ABCDEFGHIJKLMNOPQRSTUVWXYZxyzApplicant() = value

Step 2:
Find What: ###(?:(A)BCDEFGHIJKLMNOPQRSTUVWXYZa|A(B)CDEFGHIJKLMNOPQRSTUVWXYZb|AB(C)DEFGHIJKLMNOPQRSTUVWXYZc|ABC(D)EFGHIJKLMNOPQRSTUVWXYZd|ABCD(E)FGHIJKLMNOPQRSTUVWXYZe|ABCDE(F)GHIJKLMNOPQRSTUVWXYZf|ABCDEF(G)HIJKLMNOPQRSTUVWXYZg|ABCDEFG(H)IJKLMNOPQRSTUVWXYZh|ABCDEFGH(I)JKLMNOPQRSTUVWXYZi|ABCDEFGHI(J)KLMNOPQRSTUVWXYZj|ABCDEFGHIJ(K)LMNOPQRSTUVWXYZk|ABCDEFGHIJK(L)MNOPQRSTUVWXYZl|ABCDEFGHIJKL(M)NOPQRSTUVWXYZm|ABCDEFGHIJKLM(N)OPQRSTUVWXYZn|ABCDEFGHIJKLMN(O)PQRSTUVWXYZo|ABCDEFGHIJKLMNO(P)QRSTUVWXYZp|ABCDEFGHIJKLMNOP(Q)RSTUVWXYZq|ABCDEFGHIJKLMNOPQ(R)STUVWXYZr|ABCDEFGHIJKLMNOPQR(S)TUVWXYZs|ABCDEFGHIJKLMNOPQRS(T)UVWXYZt|ABCDEFGHIJKLMNOPQRST(U)VWXYZu|ABCDEFGHIJKLMNOPQRSTU(V)WXYZv|ABCDEFGHIJKLMNOPQRSTUV(W)XYZw|ABCDEFGHIJKLMNOPQRSTUVW(X)YZx|ABCDEFGHIJKLMNOPQRSTUVWX(Y)Zy|ABCDEFGHIJKLMNOPQRSTUVWXY(Z)z)
Replace With: $1$2$3$4$5$6$7$8$9$10$11$12$13$14$15$16$17$18$19$20$21$22$23$24$25$26

Now, the result will be final:

getMyApplicant() = value
getNewValue() = value
getXyzApplicant() = value
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

New answer [will be in v1.47]

[It (Support \U \u \L \l replace modifiers) is in the Insiders' Build as of June 30, 2020 so it will be in 1.47 due out later in same week.]

(see https://github.com/microsoft/vscode/pull/96128: Initial implementation: Support \U \u \L \l replace modifiers).

Find/replace support for the case modifiers \U, \u, \L and \l is coming very soon. Their use is not supported yet in the search across files functionality but that is planned.

Find : (\w+\.\w+\.)(\w)(\w+)

Replace : $1get\u$2$3() -- the PR to support \u has been merged

See https://regex101.com/r/jZlO1D/1

For more on how the case modifiers work in vscode, see https://stackoverflow.com/a/62270300/836330



Just set up a snippet to do the replacement for you. Why? Because snippets can transform to upper and lower case and capitalize capture groups easily. Plus, although you don't need it here, they can also do conditional replacements which can be extremely powerful down the road for you.

Here is a simple snippet:

{
  "key": "shift+alt+y",                          // whatever keybinding you choose
  "command":  "editor.action.insertSnippet",
  "args": {

        // see how simple this regex can be after you have found and selected your matches
    "snippet": "${TM_SELECTED_TEXT/(.*\\.)(.*)/$1get${2:/capitalize}()/g}"

  },
  "when": "editorTextFocus && editorHasSelection"
},

You still have to find what you want to change, you can use your original regex if you want

\w+\.\w+\.(\w)(\w+)

I suggest (.+\.)([^\.\s]+)(?!.*\.) see https://regex101.com/r/NbXgs3/2 it handles a few edge cases.

And allows you to have deeper structures like registerForm.main.subMain.myApplicant, etc. up to any number or fewer like registerForm.myApplicant.

  • Step 1: Input your regex into the Find widget.
  • Step 2: Alt+Enter selects all your find matches.
  • Step 3: Trigger your snippet above, here Shift+Alt+Y

Pretty easy. And then just keep the snippet around as a skeleton to use for future find/replaces with case or conditional transforms. Usually the interior regex - within the snippet, here (.*\\.)(.*) - is relatively simple since it is only acting on your pre-selected entries already and thus the transform is simpler to grok.

getFunction snippet demo

Mark
  • 143,421
  • 24
  • 428
  • 436