Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for multiple strings in several files with Sublime 3 using AND

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.

like image 789
Gabriel Avatar asked Apr 25 '16 16:04

Gabriel


People also ask

How to search for multiple strings in multiple files in sublimetext?

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: This works perfectly for searching files where either string1 OR string2 is present.

How to search for a specific keyword in Sublime Text?

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> Share

Does Sublime Text support searching in all open folders?

3 Answers 3 ActiveOldestVotes 46 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.

Can I have multiple documents open at once in Sublime Text?

Having multiple documents open at once can be a great help if you need to keep referring back to another document. For example, if you’re trying to code a custom function that’s defined in a separate file. If you want to see more than one document at the same time in Sublime Text 3, you could drag a tab out into a second window.


1 Answers

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) 
like image 85
rock321987 Avatar answered Nov 11 '22 20:11

rock321987