7

This previous (similar) question of mine Search for multiple strings in several files with Sublime 3 was answered with a way to search for multiple strings in multiple files in SublimeText, using the regex OR operator:

Find: (string1|string2)
Where: <open folders>

This works perfectly for searching files where either string1 OR string2 is present. What I need now is to search in lots of files for both strings present. I.e., I need to use the AND operator.

I looked around this question Regular Expressions: Is there an AND operator? and also this one Regex AND operator and came up with the following recipes:

(?=string1)(?=string2)
(?=.*string1)(?=.*string2)
(string1 string2)
(string1\&string2)

but none of them work.

So the question is: how can I search multiple strings in several files at once with SublimeText?

(I'm using SublimeText 3103)

Add: the strings are not necessarily in the same line. They can be located anywhere within each file. For example, this file:

string1 dfgdfg d dfgdf

sadasd
asdasd

dfgdfg string2 dfgdfg

should trigger a match.

Community
  • 1
  • 1
Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • actually it was the previous match that was highlighting..its not working for me either..its only finding in the current file – rock321987 Apr 25 '16 at 16:51
  • Press `Shift+Ctrl+F` and enter the regex.. It is finding correctly in there – rock321987 Apr 25 '16 at 16:57
  • Which regex? What files are you using to test this? – Gabriel Apr 25 '16 at 16:58
  • `(?=.*string1)(?=.*string2)` regex..on pressing `Shift+Ctrl+F` you will get add folder option..add the folder containing all files..all the matches along with filepath will be displayed in a file – rock321987 Apr 25 '16 at 16:59
  • sublime may `crash` if there are too many files – rock321987 Apr 25 '16 at 17:02
  • As I said previously, that regex returns zero matches in my case, so it is not doing what I need. I don't know what files you are using where it works. You'd need to share them so I can see. – Gabriel Apr 25 '16 at 17:49
  • @rock321987 you shouldn't have deleted your answer, it was a good one. Just not the particular answer for this question, but a good one nonetheless. – Gabriel Apr 25 '16 at 18:24
  • use `(?=[\s\S]*string1)(?=[\s\S]*string2)[\s\S]+` regex..it works but is killing the sublime for too many files – rock321987 Apr 25 '16 at 18:25

1 Answers1

10

Open sublime Text and press

Shift+Ctrl+F

or click on the Find in Files options under Files tab. The above is keyboard shortcut for this option. When you press above key, these are following options

enter image description here

When you select ... button from above, you get 6 options which are Add Folder or Add Open Files or Add Open Folders

To search strings that occur in the same line

Use the following regex for your and operation

(?=.*string1)(?=.*string2)

I am using the following regex

(?=.*def)(?=.*s)\w+ <-- \w+ will help in understanding which line is matched(will see later)

and I am searching within current open files

enter image description here

Make sure the Use Buffer option is enabled (one just before Find). It will display the matches in a new file. Also make sure the Show Context (one just before Use Buffer) option is enabled. This will display the exact line that matches. Now Click on Find on the right side.

Here is the output I am getting

enter image description here

See the difference in background color of line 1315 and 1316(appearing in left side). 1316 is matched line in designation file

This is the image of last part

enter image description here

There were total 6 files that were opened while I used this regex

For finding strings anywhere in file

Use

(?=[\s\S]*string1)(?=[\s\S]*string2)[\s\S]+

but it will kill sublime if number of lines increases.

If there are only two words that you need to find, the following will work super fast in comparison to above

(\bstring1\b[\S\s]*\bstring2\b)|(\bstring2\b[\S\s]*\bstring1\b) 
Gabriel
  • 40,504
  • 73
  • 230
  • 404
rock321987
  • 10,942
  • 1
  • 30
  • 43
  • Which two strings are you searching for with the regex `(?=.*def)(?=.*s)`? – Gabriel Apr 25 '16 at 18:06
  • @Gabriel yes, I am searching for string `def` and string `s` – rock321987 Apr 25 '16 at 18:06
  • Try searching for `(?=.*string1)(?=.*string2)` in the following mock file http://pastebin.com/Tq6YJUfC It will not work and it should. I'm not sure what your line does, but it is not what I need. – Gabriel Apr 25 '16 at 18:15
  • @Gabriel it is finding line by line in the file..i.e. string1 and string2 both should be in same line – rock321987 Apr 25 '16 at 18:20
  • Well that's it then, I'm not after strings that are *in the same line*. They could be located anywhere within each file. – Gabriel Apr 25 '16 at 18:21
  • 1
    Those last two commands are great, thank you! Could you add the commands to perform the same search but *not* display such large chunks of file where the strings were found? – Gabriel Apr 25 '16 at 20:15
  • @Gabriel I don't understand why using `and`(2nd last regex) does not display the results in all currently opened file. The last regex displays all the matched lines. I am not sure about line numbers if it can be displayed for matched lines – rock321987 Apr 26 '16 at 03:04
  • I don't understand your cmmnt. Both last commands work great, and the last one is indeed faster. The only issue I find is that it displays large chunks of each file matched, and I like to get as little as possible, perhaps only the matched file's title. – Gabriel Apr 26 '16 at 03:07
  • @Gabriel I am sorry but I don't know (if there is) any way to display the file names. I will try to find it out – rock321987 Apr 26 '16 at 03:10