Vscode: Multiline find in file (Ctrl+F) does not work for \s or [^] regular expressions

Created on 11 Jul 2016  路  13Comments  路  Source: microsoft/vscode

  • VSCode Version: 1.3
  • OS Version: Win 8/10

Version 1.3 supported Multiline Find, but Regular Expression seems not work completely.

  • \s doesn't match line breaks.
  • [^] doesn't match line breaks. We have to use [\s\S\n] instead of [^] to make it match any character.
bug editor-find

Most helpful comment

Sorry if the following is not totally related but... I just found that multiline regular expressions work with "Find" but not with "Find/Replace in Files".

With "Find" (works as expected):
screen shot 2016-07-20 at 15 11 05

With "Find/Replace in Files" (does not work as expected):
screen shot 2016-07-20 at 15 11 52

My regular expression is ^<p>Hello(.|\n|\r)+<p>Hello Solar System!<\/p> and my text is:

<p>Hello world!</p>
<p>Hello Solar System!</p><p>Hello Galaxy!</p>

I'm using Visual Studio Code 1.3.1 on OSX El Capitan (10.11.5)

All 13 comments

@anseki We use JavaScript's regular expression engine behind the scene, so the supported syntax should be that of JavaScript regex. We do not have plans to switch to a different regular expression engine.

At least, V8 in Electron seems to interpret regular expressions correctly.
For example:
VS Code menu "Help" -> "Toggle Developer Tools" -> "Console"

'<A\nB\nC>'.replace(/<[ABC\s]*>/, 'OK')
'<A\nB\nC>'.replace(/<[^]*>/, 'OK')

Two strings that include line breaks were replaced correctly, and OK was shown.

I think, if regular expression of VS Code differs from JavaScript, the information should be documented. Because the pattern matches a string which doesn't include line break, we might overlook something important.

@anseki Sorry about that! Now I understand what's going on.

To optimize on memory usage we do the following:

  • here we preparse the search string and look for \n or \r. If we find \n or \r in the search string we assume this was a multiline search
  • then, to optimize memory usage (not concatenate the entire buffer into a string) we have two search strategies, either we search line by line (if the search string indicates this is not a multiline search) or we search in the concatenated string.

Looks like the heuristic is wrong, as \s and [^] should also use the multiline version of the find matches.

Sorry, my wrong English...

I read that code and I thought that this issue is difficult, because it can't decide whether the pattern get multiline or not by only keyword or characters.
For example, [^x], \x0a, [\x09-\x0b] and other patterns also match line break.
I think, only some patterns that are commonly used such as \s and [^] should be supported. And the information should be documented.

[\s\S] and [^] are used to make it match any character.
For example, HTML comment: <!--[^]*?-->

Sorry if the following is not totally related but... I just found that multiline regular expressions work with "Find" but not with "Find/Replace in Files".

With "Find" (works as expected):
screen shot 2016-07-20 at 15 11 05

With "Find/Replace in Files" (does not work as expected):
screen shot 2016-07-20 at 15 11 52

My regular expression is ^<p>Hello(.|\n|\r)+<p>Hello Solar System!<\/p> and my text is:

<p>Hello world!</p>
<p>Hello Solar System!</p><p>Hello Galaxy!</p>

I'm using Visual Studio Code 1.3.1 on OSX El Capitan (10.11.5)

Hi @nunoarruda,

Strangely enough, it seems to find files in "SEARCH" sidebar when the files are opened by editor. (of course "Find" in editor also find that.)
For example, I tried to find files by <html>[\r\n\s]*<head> at a folder that contains HTML files.
This regular expression should match string in those files. But "No results found" was shown.
I opened one HTML file, and clicked "Refresh" button in "SEARCH" sidebar. Then the file was shown in result list.
And then, I opened one more HTML file, and clicked "Refresh" button. Then two files were shown in result list.

@anseki I've tried what you've reported with my example and I can reproduce. It matches (but incorrectly) when the file is open but it does not match when the file is closed.

The regex find in VS Code is definitely buggy, needs fixing.

@nunoarruda @anseki The search viewlet goes through different code paths:

  • when a file is opened it searches using the same search implementation as the find widget (thus multi-line works)
  • when a file is not opened it searches in a separate process with a different implementation.

Please create a separate issue for aligning multiline find in the search viewlet.

This will be addressed with #13155, right?

@roblourens what does "global search" mean in #13155? I thought it was about searching in multiple files, which this issue is not, but please correct me if I'm wrong.

@rebornix This might be a non-issue with our new textbuffer / search implementation?

i.e. there is no longer a need to optimize searching line-by-line

No longer see above issues as our new text buffer doesn't try to do line by line optimization anymore.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sirius1024 picture sirius1024  路  3Comments

philipgiuliani picture philipgiuliani  路  3Comments

mrkiley picture mrkiley  路  3Comments

chrisdias picture chrisdias  路  3Comments

omidgolparvar picture omidgolparvar  路  3Comments