454
%s@{fileID: \(213[0-9]*\)@\='{fileID: '.(submatch(1)-1900)@

I am using this regex search and replace command in vim to subtract a constant from each matching id.

I can do the regex find in VSCode but how can I reference the submatch for maths & replace? submatch(1) does not work in VSCode?

Thanks.

Abdollah
  • 4,579
  • 3
  • 29
  • 49
Rakka Rage
  • 15,941
  • 8
  • 33
  • 45

8 Answers8

730

Given a regular expression of (foobar) you can reference the first group using $1 and so on if you have more groups in the replace input field.

Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54
  • 2
    asked again diffrently here: http://stackoverflow.com/questions/35283300/vscode-sub-match-evaluate-instead-of-concatenate – Rakka Rage Feb 09 '16 at 14:58
  • 12
    I had to replace `

    (.*)

    ` with `

    \n\t\t\t\t\t\t$1\n\t\t\t\t\t

    ` for a 2000 line file. Imagine me doing it manually.
    – Bishwas Mishra Jun 19 '19 at 16:40
  • 35
    ... and $0 to reference the full matched string (no parentheses needed). This is useful if you want to add to the search string, as in replacing `"Bond"` by `"$0, James $0"`. – sferencik Nov 30 '20 at 13:56
  • 2
    Dont forget to click the .* button to tell vsc you entering regex – Gilbert Jan 08 '21 at 08:42
  • 2
    ~~What if my replacement string has a number right after the reference? E.g. replace _(6)_ with _678_? `$178` would obviously not work. In some implementations there is [a special syntax](https://stackoverflow.com/a/811140/2822719) (`${1}78`) for that, but at least that one doesn't work in VS Code. Do you happen to know what will?~~ | **Nevermind, `$178` just magically works! ‍♂️** – Marcus Mangelsdorf May 23 '22 at 15:20
204

To augment Benjamin's answer with an example:

Find        Carrots(With)Dip(Are)Yummy
Replace     Bananas$1Mustard$2Gross
Result      BananasWithMustardAreGross

Anything in the parentheses can be a regular expression.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
114

Just to add another example:

I was replacing src attr in img html tags, but i needed to replace only the src and keep any text between the img declaration and src attribute.

I used the find+replace tool (ctrl+h) as in the image: Find and replace

Diogo Paschoal
  • 1,507
  • 1
  • 14
  • 16
  • 15
    for me this works inconsistently - though selection forks correct, the output sometimes keeps `$1` without replacement – godblessstrawberry Oct 08 '18 at 08:26
  • 1
    Hi, @godblessstrawberry, can you share your search and replacement strings? – Diogo Paschoal Oct 09 '18 at 17:49
  • 3
    hi @Diogo I was trying to replace projectwide `\[innerHtml\]\s*=\s*"'(.*)'\s*\|\s*translate\s*"` with `myTranslate="$1"` and it was skipping keys randomly and inserting $1 instead of group value sometimes. update to 1.28.0 resolved this issue – godblessstrawberry Oct 10 '18 at 07:17
  • I ran into the same problem as @godblessstrawberry, doing a global replace across all my files with the regex ' (and|or\+|\-)\n( *)' and the replacement string '\n$2$1 ' sometimes correctly shifted the binary operator to the start of the newline but in other cases just inserted $2$1. Would appear to be a bug in VSCodium – Tom Close Oct 02 '19 at 01:55
  • @godblessstrawberry, check this: https://github.com/microsoft/vscode/issues/81825. If it do not apply for you, provide more info and open an issue there. – Diogo Paschoal Oct 04 '19 at 18:33
  • This regex is a bit dangerous as it won't work when there are multiple `img` tags in one line or the three letters `img` appear somewhere in the text. Something like `(]*)(src=)` might be a better solution. – Daniel Hauck Apr 08 '22 at 10:38
59

For beginners, the accepted answer is correct, but a little terse if you're not that familiar with either VSC or Regex.

So, in case this is your first contact with either:

To find and modify text,

  1. In the "Find" step, you can use regex with "capturing groups," e.g. I want to find (group1) and (group2), using parentheses. This would find the same text as I want to find group1 and group2, but with the difference that you can then reference group1 and group2 in the next step:

  2. In the "Replace" step, you can refer to the capturing groups via $1, $2 etc, so you could change the sentence to I found $1 and $2 having a picnic, which would output I found group1 and group2 having a picnic.

Notes:

  • Instead of just a string, anything inside or outside the () can be a regular expression.

  • $0 refers to the whole match

ultraGentle
  • 5,084
  • 1
  • 19
  • 45
28

In my case $1 was not working, but $0 works fine for my purpose.

In this case I was trying to replace strings with the correct format to translate them in Laravel, I hope this could be useful to someone else because it took me a while to sort it out!

Search: (?<=<div>).*?(?=</div>)
Replace: {{ __('$0') }}

Regex Replace String for Laravel Translation

Ares9323
  • 846
  • 1
  • 12
  • 20
  • 1
    Is that possible for you copy your edit your post and copy the code instead of the image? It would be better for visualization. – Pankwood Dec 13 '19 at 13:49
8

Another simple example:

Search: style="(.+?)"
Replace: css={css`$1`}

Useful for converting HTML to JSX with emotion/css!

Hedley Smith
  • 1,307
  • 15
  • 12
7

Just another example for someone figuring out.

In this example, I've added #### to the start of the string and placed the first group $1 after that. Everything outside group (.*) is going to be deleted.

<h4.*">(.*)</h4>
#### $1

enter image description here

# before: 
<h4 id="extract-inline-json-with-regex">Extract inline JSON data with Regex</h4>

# after:
#### Extract inline JSON data with Regex
Dmitriy Zub
  • 1,398
  • 8
  • 35
0

In "files to include"

./REPONAME/**/{application_stress*,application_k8s*}
./REPONAME/**/application_*{stress*,k8s*}
./REPONAME/**/{*_stress*,*_k8s*}