33

I know how to search for a single string in several files at once with Sublime 3 (explained here).

What I need to do is to search for multiple strings in several files. I've tried going to Find in files and setting:

Field:  string1 \& string2
Where: /path_to_folder_containing_the_files_I_want_to_ search/

(where string1 and string2 are the strings I want to search for) but that doesn't seem to work.

Can this be done at all?

Community
  • 1
  • 1
Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • Gabriel, I think your question would be better on http://superuser.com – samy Sep 05 '14 at 15:26
  • @samy why? There are thousands of questions regarding `Sublime` here, just click on the tags to see. – Gabriel Sep 05 '14 at 15:27
  • 2
    Yeah I agree; this looked like a question about general computing software to me, not programming - so I was thinking that the sublime tag on superuser was most logical: http://superuser.com/questions/tagged/sublime-text-3 – samy Sep 05 '14 at 15:30

3 Answers3

51

Just came across this old question and thought I'd add my solution, which may be useful for somebody in the future.

Sublime supports searching in all open folders and can use regex. So utilizing both, you can open or add all folders you want to search in to the project and use regex to search for multiple keywords. In your case it would be the following (make sure to check the regex box .* icon):

Find: (string1|string2)
Where: <open folders>
Sherzod
  • 5,041
  • 10
  • 47
  • 66
  • When using a regex in the place of 'string1', 'string2' on 4 strings or more, regex crashes with the error: "The complexity of matching the regular expression exceeded predefined bounds. Try refactoring the regular expression to make each choice made by the state machine unambiguous." Is this relate to this type of search? – john Dec 08 '16 at 16:17
  • 1
    @john: can you post your sample regex? Just tried with simple 4 options, and it works just fine on mine. – Sherzod Dec 08 '16 at 18:32
  • 4
    apparently I was regex'ing each individual string, rather than encapsulating the entire set as a whole, and that caused the crash. E.g. I wanted this: `^.*(?:string1|string2|string3).*$` – john Dec 10 '16 at 04:44
6

I use this:

checked Regular expression

Find: (string1.*string2)

Where: *.php
koshin
  • 161
  • 1
  • 5
  • 1
    This will only work if a file has *both* strings, and in the order given. (Also, the dot does not match newlines in some regex scenarios...not sure about Sublime's implementation) Better to use | (or operator): (string1|string2) – David Hempy Sep 17 '18 at 14:27
  • But if i want to find both strings my solution will work. – koshin Sep 19 '18 at 05:35
  • 1
    If you want to find them *in that order*, yes. But if a file has them reversed: string2...string1, this solution will not match that. – David Hempy Sep 19 '18 at 18:56
0

I tried this on Sublime Text2, so should work on Sublime Text3 as well.

Field: string1 string2 string3 string4 Where: /path_to_folder_containing_the_files_I_want_to_ search/

Note: uncheck '.*' which means Regular Expression and check "" which means look for the whole word.

This will search for the pattern "string1 string2 string3 string4" in all the files in the mentioned folder.

Anamitra
  • 9
  • 1