vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Replace in files doesn't work as expected with match containing newline

Open jeiea opened this issue 2 years ago • 5 comments

Type: Bug

Steps to reproduce

  1. Create new file and fill the following.
child: asdf; asdf
}
  1. Press Cmd+Shift+H, turn on regex option (Cmd+Option+R), fill find text with child: (\w+);(?=[\S\s\n]*?\}), replacement text with $1.

  2. See preview or do replace.

Expected behavior

child: asdf should be replaced with asdf.

Actual behavior

child: asdf is replaced with $1. image

Additional context

Normal single file replace works as expected.

If match contains no newline, it also works as expected.

VS Code version: Code - Insiders 1.75.0-insider (b229eb5fa2ecadb8a5238ccb9f6f49f7ccba9d2b, 2023-01-27T22:17:24.641Z) OS version: Darwin x64 22.1.0 Modes: Sandboxed: Yes

System Info
Item Value
CPUs Intel(R) Core(TM) i5-8500 CPU @ 3.00GHz (6 x 3000)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
metal: disabled_off
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
Load (avg) 2, 2, 6
Memory (System) 24.00GB (0.02GB free)
Process Argv
Screen Reader no
VM 0%
Extensions: none
A/B Experiments
vsliv695:30137379
vsins829:30139715
vsliv368:30146709
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vswsl492:30256197
vslsvsres303:30308271
pythonvspyl392:30422396
pythontb:30258533
pythonptprofiler:30281269
vshan820:30294714
pythondataviewer:30285072
vscod805:30301674
bridge0708:30335490
bridge0723:30353136
cmake_vspar411:30581797
vsaa593cf:30376535
pythonvs932:30404738
cppdebug:30492333
vsclangdf:30492506
c4g48928:30535728
dsvsc012cf:30540253
pynewext54:30618038
pylantcb52:30590116
pyindex848:30611229
nodejswelcome1:30587009
pyind779:30611226
pythonsymbol12:30651887

jeiea avatar Jan 31 '23 01:01 jeiea

I'll put a note here.

Problem

For me regex match group substitution was not working on Unicode characters.

image

Replace match groups with $X ($0 $1, $2, $3 ...) failed.

image

After testing, I removed "This is a test-", line, and now it's not even finding a match in the file even though "കണ്ടന്നുമുതൽ തൻ-", sill exists.

Solution

But then I learned from here even though \w matches Unicode characters, it is preferable to use \p{L}.

image


Edit:

It still does not match every Unicode alphabet.

image

I've trying to capture every Unicode alphabet succeed by a - without space in between.

image

yozachar avatar Jul 29 '23 03:07 yozachar

@jeiea, in your case, maybe you've to escape the \ before matching \n like so: \\n?

Given:

child: asdf; asdf
}

If you've to replace child: asdf with asdf why use regex at all? You could simply do:

F: child: asdf; R: asdf;

yozachar avatar Jul 29 '23 03:07 yozachar

That's simplified example for the reporting. I don't remember original use case but that's not important. Just because asdf can be anything, I had to use regex.

jeiea avatar Aug 01 '23 22:08 jeiea

But then I learned from here even though \w matches Unicode characters, it is preferable to use \p{L}.

I see that \p{L} is syntax only used in PCRE, but open files should use the JS engine for processing regexes (https://github.com/microsoft/vscode/wiki/Search-Issues#notes-on-regular-expression-support). This being said, you likely need to look for something that works for both engines and is clean, unfortunately

andreamah avatar Dec 05 '23 20:12 andreamah

I found a simpler repro for this:

In the single file find & replace, replace regex \s with and nothing will happen to the newlines in the file

alexanderwu-db avatar Jun 28 '24 20:06 alexanderwu-db