Search in VS Code for multiple terms

RegexSearchVisual Studio-Code

Regex Problem Overview


Suppose I search on VS Code the terms 'word1 word2'. Then it finds all the occurrences where 'word1' is followed by 'word2'. In reality I want to find all the files where word1 and word2 occur, but they don't have to be consecutive. How can I do it?

Regex Solutions


Solution 1 - Regex

Use regex flag and search for (word1[\s\S\n]*word2)|(word2[\s\S\n]*word1)


Made a small extension based on @tonix regex:

https://marketplace.visualstudio.com/items?itemName=usernamehw.search

Solution 2 - Regex

Here is also a simple way for simple needs - use this as regex

(word1)|(word2)|(word3)  

It may not cover some cases, but has been working fine for me, and easy to remember to type it in.

Solution 3 - Regex

VSCode has an open issue to support multiple searches. You may want to get on there and push them a little.

Solution 4 - Regex

For you guys,

if you want to search for multiple words (more than 2) at once in a single file and all the words must appear in the file at least once (logical AND), you can use the following regex which leverages lookahead assertions:

^(?=[\s\S\n]*(word1))(?=[\s\S\n]*(word2))(?=[\s\S\n]*(word3))(?=[\s\S\n]*(word4))[\s\S\n]*$

A global search with this pattern will only return all the files that contain word1 AND word2 AND word3 AND word4 in any order (e.g. word4 may appear at the beginning and/or word2 may appear at the end of the file).

I also wrote a little Python CLI helper which creates the regex automatically for you given the patterns you want to AND (though creating the regex by hand is pretty straightforward).

Copy the following code, paste it in a new file and save it somewhere on your machine (I've called it regex_and_lookahead.py). Then make the file executable with chmod +x ./regex_and_lookahead.py (important, I used Python 3.6, the literal prefix f -> f'(?=[\s\S\\n]*({arg}))' won't work in previous versions):

#!/usr/bin/env python
from sys import argv

args = argv[1:]
regex = '^'
for arg in args:
    regex += f'(?=[\s\S\\n]*({arg}))'
regex += '[\s\S\\n]*$'

print(regex)

Usage:

./regex_and_lookahead.py word1 word2 word3 word4

Will generate the above regex. You can also use it to generate more complex regexes cause each parameter can have regex characters in it!

As an example:

./regex_and_lookahead.py "pattern with space" "option1|option2" "\bword3\b" "(repeated pattern\.){6}"

Will generate the following regex:

^(?=[\s\S\n]*(pattern with space))(?=[\s\S\n]*(option1|option2))(?=[\s\S\n]*(\bword3\b))(?=[\s\S\n]*((repeated pattern\.){6}))[\s\S\n]*$

Which will match a file if and only if all of the following conditions are true:

  • There's at least one occurrence of the string pattern with space;
  • There's at least one occurrence of either option1 or option2;
  • There's at least one occurrence of the word word3 delimited by word boundary assertions;
  • There is at least one occurrence of the string repeated pattern. repeated 6 times (i.e.: repeated pattern.repeated pattern.repeated pattern.repeated pattern.repeated pattern.repeated pattern.).

As you can see, the sky is the only limit. Have fun!

Solution 5 - Regex

This is now supported, you can search for the term then open in editor and use ctrl + f to search the search results thanks @pushkin

Solution 6 - Regex

To apply logical and (?=.*word1)(?=.*word2)(?=.*word3)

To apply logical or (word1)|(word2)|(word3)

Solution 7 - Regex

Try Open new Search Editor command, through command pallete, You can map it to any keybinding you'd like in the Keybindings Editor. I mapped to cmd+shift+i

enter image description here

This is helpful for me!

There is one more way, using up/ down arrow key in search editor, moves us across our search history, even this is useful, >It needs a little bent of mind to accept that it is equivalent to having multiple search editors (what IntelliJ etc provides) but without persistence!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbeckoView Question on Stackoverflow
Solution 1 - RegexAlexView Answer on Stackoverflow
Solution 2 - RegexKirill YunussovView Answer on Stackoverflow
Solution 3 - RegexpushkinView Answer on Stackoverflow
Solution 4 - RegextonixView Answer on Stackoverflow
Solution 5 - RegexChagai FriedlanderView Answer on Stackoverflow
Solution 6 - RegexAmol PolView Answer on Stackoverflow
Solution 7 - RegexAkshay Vijay JainView Answer on Stackoverflow