1

I want to run a regex to find instances in my code where I declare an animation duration time: e.g. duration: 2000 and replace them with that duration plus an added variable timeScale, e.g. duration: timeScale * 2000

Some of my codebase already has this - so I want to find all instances of duration: that are not followed by timeScale.

I know I can find things that don't exist by saying: ^((?!timescale).)*$

But how do I couple that (or something similar) with the thing that does exist (duration:)?

UPDATE:

I can actually run this in a regex tester:

https://regex101.com/r/vY3pY3/1

duration: (?!timescale)

But how do I run it in VSCode? It gives errors saying unrecognized flag !

enter image description here

mheavers
  • 29,530
  • 58
  • 194
  • 315
  • Negative lookahead is `(?!timeScale)`, not `(?!=timeScale)`, you have an extra equal sign. – Toto Jun 22 '18 at 13:19
  • 1
    Update your VSCode and use `^(?!.*timescale).*duration:`. Not sure why lookahead is not accepted in your case. – Wiktor Stribiżew Jun 22 '18 at 13:27
  • @WiktorStribiżew - It doesn't seem to like the use of `!` in this instance either. Interestingly, if I just put `^(?!.*timescale)` it doesn't throw an error, but it says it matches everything – mheavers Jun 22 '18 at 13:40
  • @WiktorStribiżew - your solution, coupled with setting useRipGrep to false, worked for me! If you post it as an answer I will accept it. – mheavers Jun 23 '18 at 17:57
  • @mheavers [Posted, see below](https://stackoverflow.com/a/51003768/3832970). – Wiktor Stribiżew Jun 23 '18 at 18:02

3 Answers3

2

Find in files functionality (CTRL+ALT+F) seems to use a different regex flavor in contrast to Find (CTRL+F) functionality in VSC (Weird!). You may want to try the latter which supports lookaheads. From documentations:

VS Code does support regular expression searches, however, backreferences, lookaround, and multiline matches are not supported. This is because VS Code depends on the search tool ripgrep, which, while extremely fast, doesn't support these advanced regex features.

This behavior would be completely unexpected. You may also try to set:

"search.useRipgrep": false

which I think may work or may not.

revo
  • 47,783
  • 14
  • 74
  • 117
  • Just says 'No Results' – mheavers Jun 22 '18 at 14:28
  • 1
    This behavior is completely unexpected. You may also want to try `"search.useRipgrep": false` which I think may work or may not. In fact they have changed the regex engine to something less useful. What's the reason? I don't know. – revo Jun 22 '18 at 14:34
  • Interestingly, setting `useRipgrep` to false got rid of the error, but still returned no results. – mheavers Jun 23 '18 at 17:53
  • Couldn't you just use another tool for this search / replace task? – revo Jun 23 '18 at 18:25
1

As per your feedback, make sure you set useRipGrep to false and use

^(?!.*timescale).*duration:

Details

  • ^ - start of line
  • (?!.*timescale) - a negative lookahead that fails the match when there is timescale substring after any 0+ chars other than line break chars
  • .* - any 0+ chars other than line break chars as many as possible
  • duration: - a duration: substring

See the regex demo.

NOTE: Starting with VSCode 1.29, you need to enable search.usePCRE2 option to enable lookaheads in your regex patterns.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Try Regex: ^(?:(?!timeScale).)*(duration: \d+)$

Replace with: $1 * timeScale

Demo

This works in Visual Studio Code 1.22.2

Matt.G
  • 3,586
  • 2
  • 10
  • 23
  • Well, I want to be able to match durations of any numerical value, not just 2000. Also - getting same error about the `!` if I paste that into the search window – mheavers Jun 22 '18 at 13:11
  • @mheavers, I have updated the regex and demo to accept any numerical value. Also, which is the version of Visual Studio Code that you are using? – Matt.G Jun 22 '18 at 13:27
  • I'm using 1.24.1. Can you look at the screenshot in my question and see if it matches yours (minus the error) – mheavers Jun 22 '18 at 13:35
  • @mheavers, when I do Ctrl+H (find & replace in current file), it works. however, I get the same error as yours, when I do Ctrl + Shift + H (find and replace in files). – Matt.G Jun 22 '18 at 15:17