Grant Curell
Grant Curell

Reputation: 1763

Bizarre problem with vscode regex - vscode not matching simple letter?

Visual studio code under certain circumstances doesn't seem to match letters and I'm not sure if I'm missing something incredibly obvious or if this is a bug. In my case I'm standardizing a code base and I want to change things like:

}
else {
  Write-Warning

to:

} else {
  Write-Warning

I wrote the following regex:

((?:(?=[^\r\n])\s))\}
.*else \{
(there's a newline here you can't see)

to identify the lines I want to change. On one file it worked just fine:

enter image description here

However on the next file I got the following:

enter image description here

As you can see I changed the regex a bit, but it works as expected and matches everything up to and include the 'e'. However, if I add an l:

enter image description here

It no longer matches. I tried copying and pasting the 'l' thinking maybe it was some weird unicode problem or something - still doesn't match. Am I missing something?

Update

I checked the line endings with a hex editor and confirmed everything was only 0A (LF). There were no carriage returns:

enter image description here

I also confirmed in Visual Studio code the line endings were set to LF.

enter image description here

Upvotes: 1

Views: 391

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

If you use a regex for the task, consider changing yours to

([^\S\r\n])\}\n[^\S\r\n]*else[^\S\r\n]*\{

Replace with $1} else {.

See the regex demo.

Details

  • ([^\S\r\n]) - Group 1: any whitespace but \r and \n
  • \}\n - a } and any line break (that is how \n works in VSCode regex search field)
  • [^\S\r\n]* - any 0 or more whitespaces but \r and \n
  • else - a word else
  • [^\S\r\n]* - any 0 or more whitespaces but \r and \n
  • \{ - a { char.

Demo:

enter image description here

Upvotes: 1

Related Questions