Sublime Forum

Searching for multiple strings across several files in AND mode?

#1

I am aware that I can search in ST3 for multiple strings across many files as a regex OR operation using (string1|string2|string3), but how can I search for all my key words in an AND operation?

0 Likes

#2

regex isn’t really designed for this, but you may be able to do it albeit with potentially disastrous performance…

(?<and>string1|string2|string3){0}(?<between>[\s\S]*?){0}(?<result1>(?&and))(?&between)(?<result2>(?!\k<result1>)(?&and))(?&between)(?!\k<result1>|\k<result2>)(?&and)[\s\S]*+\Z
1 Like

#3

Thanks. So does that mean there is no user-friendly support for such a search in ST3?
Tools such as NirSoft’s excellent freeware SearchMyFiles allow just this kind of thing - in SearchMyFiles’s case, you simply supply comma separated search items to be searched for in either AND or OR mode.

0 Likes

#4

Prose writer here. If I’m looking for a way to search across all of a project’s files and find those files that contain two or more specific strings, is the above-mentioned regex approach the only/best way to do it?

Example, in a project with many files, I want to see just those in which I mention “Carol” AND “Dan” AND “Bob”

0 Likes

#5

well you can do individual searches, then delete any lines with line numbers from the find results page, just leaving the file paths of matches (or just run matches = view.find_by_selector('entity.name.filename.find-in-files'); view.selection.clear(); view.selection.add_all(matches) in the ST console and paste to a new buffer), then sort them, then search for where 3 lines in a row are identical (meaning each of the 3 searches found a match) which would be ^(.*)$(?:\n\1$){2} in regex mode.

1 Like

#6

Thanks kingkeith. Sorry for the late reply–I missed an email notification, I think.

0 Likes