Regular expression to find two words in files in folder
I need to look for two words in a set of files in a folder.
(Word1) | (word2)
I find files that contain either word1 or word2 as but I need to find
Only files that contain word1 and word2 together ignoring all other files.
Thanks for your cooperation
guy038 last edited by guy038
The regex given by @per-isakson is quite correct. However when the general case where the two words
Word2are located in different lines, a search, with the Find in Files dialog, does NOT display, in the Find Result panel, all the lines of the block, beginning with
Word1and ending with
Word2( or the opposite ) but ONLY the first line of each multi-lines block. ( small bug ! )
So, instead, you could use the regex
(?si)(Word1)(?=.*?(Word2))|(?2)(?=.*?(?1)), which searches for, either, the words
Word2, in an insensitive case way, if they are followed, further on, by the second specific word
(?si), at beginning of the regex, are modifiers which ensures that :
The dot (
.) special character matches, absolutely, any single character ( standard or EOL )
The search will be perform, in an insensitive case way ( If you need a sensitive search, just use the syntax
(Word1)matches the string
Word1, stored as group 1, due to the parentheses, ONLY IF followed by the first string
Word2, found afterwards, also stored as group 2, due to the “Look-ahead” construction
After the alternative symbol
|, the case
(?2)(?=.*?(?1))just represents the opposite case, where we’re searching for the string
Word2, followed, further on, with the string
Word1. We use a specific regex construction
(?#), named a called subpattern. ( This atomic group is just a particular case of of recursive subpattern, located outside the parentheses to which it refers )
@andrea-cappelli, if your two words,
Word2, are, always, both located in a same line, you could, preferably, use the more simple regex, below, which searches for the smaller range of characters, in a same line, between the string
(?i-s)modifiers ensures that :
The search is performed, in an *insensitive case way
The dot will match a single standard character, even if you previously checked the . matches newline option
If you need a sensitive search, change the modifiers part by the syntax
After running these regexes, using the Find in Files dialog, you should get, in the Find result panel :
The absolute path of each file, containing the two words
Some lines, containing, either,
If you, simply, need the list of all these files, follow the method, below :
With a right mouse click, choose the select All option, in the Find result panel
Ctrl + Cshortcut ( DO NOT use the context option Copy ! )
Paste the clipboard contents in a new tab, with the
Ctrl + Vshortcut
In this new tab, perform the simple S/R, below :
It’s very important to understand the fundamental difference between a subpattern, used as a subroutine and a back reference !!
For instance, given the four lines text, below :
123abc123 123abc789 789abc123 789abc789
(\d+)abc\1, with the
\1back-reference, would match the first and fourth line, only. Indeed, the syntax
\1refers to the present value of the group 1
Whereas the regex
(\d+)abc(?1), with the
(?1)called subpattern, would match the four lines ! Actually, this second regex syntax is,simply, identical to the regex
After preparing this post, I found out that a previous post, more detailed, at the address, below, just speak about a very similar problem !!!
Thank you very much for the explanation. The expression suits me in this form
Vasile Caraus last edited by Vasile Caraus
Andreea Cappelli, I test you last regex, I am glad to here that works, but I don’t know in what case exactly. I have a file with Word1 and Word2 on different lines in the same file, and doesn’t work you regex. So, tell us how it looks your file.
guy38, about your very fine regex
(?i-s)(Word1).*?(Word2)|(?2).*?(?1)this selects everything from word1 and word2 on the same line. Perfect.
Now, I change a little bit your regex, so I can select the entire line that contains Word1 and Word2.
^.*(?i-s)(Word1).*?(Word2)|(?2).*?(?1).*$but doesn’t work too good. It selects everything till Word2, but not everything after him. Can you look a little bit?
With some experience for me as it gave me the best results is this
But I see that the discussion gets interesting then I write the whole issue.
I file like this
tankitem("+ESSENZE FLOREALI (alt E)",1,1)
tankitem("+Aesculus compositum (Drops)",1,1)
tankitem("+Viscum compositum mite",1,1)
tankitem("+Aesculus compositum (Drops)",2,1)
tankitem("+Viscum compositum mite",2,1)
client_info(“NAME AND SURNAME”,"",“JESI”,"",“13.07.1926”,"",“Clinic”)
Of these files should I look for
Jesi only in the line “client_info” and 2005 only in the line “test_date”
Vasile Caraus last edited by
and where are
word2in your text?
Why do you try to match what’s between the word1 and two with
(?s:.?), which only matches zero or one character? Replace it by
test_date, respectively. That will make your example work.
word1 = Jesi
word2 = 2005
word2are names of variable rather than values, I don’t know.
guy038 last edited by
Now, I realized that the regex, given in my previous post,
(?i-s)(Word1).*?(Word2)|(?2).*?(?1)could be simplified !
Indeed, as I explained, we can’t use back-references, which are not defined if the regex engine choose the second alternative ! But, when the boundaries Word1 and Word2 are not, themselves, regexes ( as, for instance
a..z… ) and rather simple strings, we can use the more simple syntax below :
Secondly, to select any entire line ( with its EOL characters ) containing the two words
Word2, whatever their order, use the regex, below :
As we’re rather dealing with exact words, we should use, instead of the two above, the regexes, below :