0

I have looked at several posts including this question which is asking essentially the same thing, but was wrongly closed (IMO) as the close rational cites a question. asking about sub-strings within a string i.e. strings that contain one sub-string, but not another.

So I am re-posting the request here in hopes that there is a way using a single regular expression to identify within a large collection of files, those files that contain eg. "string1" , but do not contain "string2".

I am using the regular expression implementation integrated into the Sublime Text (Build 4126) to search a collection of files, and return only those files which contain at least one instance of eg. string1 and zero instances of string2.
The closest I have come to finding what I am looking for comes from the accepted answer in this question (same as linked above.)

^(?!.[\s\S]*MISSING_TERM).[\s\S]*INCLUDED_TERM.*$  

But as stated above, this only targets sub-strings within a string, not a collection of files. When I tried it on my collection of files, it found files that contained both strings rather than files that did not include the 2nd string.

ryyker
  • 22,849
  • 3
  • 43
  • 87

1 Answers1

1

I have no way of testing this in Sublime Text but if you feed one file to this regex this may be what you need to tell you if that one file matches your requirements, string1 should be present but string2 should not:

/^(?!.*string2).*string1/s
  • ^ anchor at the start of the file (note the /s modifier).
  • (?!.*string2) negative lookahead. The preceding RE (^) only matches if this (.*string2) does not match.
  • .*string1 match on anything followed by string1.

Disclaimer: Since s usually don't know anything about files I'm assuming that Sublime Text has a way of applying the to a set of files and report those matching.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Sublime accepted your proposed query syntax when I tried it. This is the search: `/^(?!.*"PLEASE RESET THE UNIT TO COMPLETE THE OPERATION").*UPDATE_STATUS/s`. Note that the sting to exclude includes spaces, so I surrounded it with `"..."`, however it did not work. I also tried with `/gm` instead of `/s`. Thanks for the attempt! – ryyker Oct 05 '22 at 13:14
  • 1
    @ryyker regex treats your literal spaces as part of the string so those `"` should not be there. – Ted Lyngmo Oct 05 '22 at 13:32
  • Thanks. Sorry I failed to mention that I had tried it without them as well. – ryyker Oct 05 '22 at 13:33
  • @ryyker I see. Perhaps Sublime Text doesn't feed the complete file to the regex. If it doesn't, I think this'll be hard to solve. – Ted Lyngmo Oct 05 '22 at 13:35
  • I wonder if I am expecting something that exceeds the scope of what `regex` is designed to provide. i.e. searching multiple files with the criteria of requiring one string, and requiring !another string may just not be queryable using this tool. Ergo the closed status. – ryyker Oct 05 '22 at 13:36
  • I have used Sublime/regex many times with successful queries on multiple file collections, but never with this criteria. I have a parser written in C that I can adapt to do what I need. Thanks again for your help here! – ryyker Oct 05 '22 at 13:39
  • 1
    Your answer, although it did not ultimately meet my needs, was useful in that it, and our disucussion helped to expand my understanding a little more on regex. +1 – ryyker Oct 05 '22 at 13:45
  • @ryyker Thanks! Regex shouldn't have a problem with it. I used the regex in the answer and matched it successfully against files where the `string2` and `string1` parts where on different lines etc. It worked fine - but it required that I had the whole file in a variable and matched against that. I did that test in `perl` since I don't have access to Sublime Text – Ted Lyngmo Oct 05 '22 at 13:45
  • Okay - I will keep on it a little more. There may be something to the way Sublime reads in the file, as you mentioned in an earlier comment. FWIW, I assume because you are online here, that internet access is not what is keeping you from having access to Sublime. It is a free download, with small license fee to silence the nags. – ryyker Oct 05 '22 at 13:51
  • @ryyker :-) Yeah, I actually started installing it on my Ubuntu WSL to check it out but it required so many packages that I didn't have and it seemed to go on downloading forever. Perhaps it's done by Tomorrow when I get back to that computer. – Ted Lyngmo Oct 05 '22 at 16:59
  • hmmm, Sorry that its not working for you. its a very quick install on Windows. I have used it since v2x and never had it take that long to install. I choose the binary installer for Win64, so does not require me to explicitly load any additional packages. FWIW - I just did a quick Google, and you are not alone. Installing for Linux seem to be problematic in general. – ryyker Oct 05 '22 at 17:08
  • @ryyker I don't have to explicitly load the packages. Ubuntu's package manager handles it fine. It's just that I've not installed much on it before except binutils, `vim`, `gcc` and `clang`. So now it had some 50 packages (and some were pretty big) to download and install. – Ted Lyngmo Oct 05 '22 at 17:12