Help with find files contents by RegEx, AND function

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
karimmaster101
Posts: 158
Joined: 20 May 2011 14:34

Help with find files contents by RegEx, AND function

Post by karimmaster101 »

For example I have two text files and its contents as below
First txt

Code: Select all

cat
dog
house
desert
Second txt

Code: Select all

cat
dog
house
sunny
boys
farm
So I want to find txt file which contains both words "house" and "farm" ignoring the order.

Thanks in advance


Note: the above example is just for explaining the idea not what I deal with

highend
Posts: 14567
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Help with find files contents by RegEx, AND function

Post by highend »

Code: Select all

farm[\s\S]*?house|house[\s\S]*?farm
One of my scripts helped you out? Please donate via Paypal

karimmaster101
Posts: 158
Joined: 20 May 2011 14:34

Re: Help with find files contents by RegEx, AND function

Post by karimmaster101 »

highend wrote:

Code: Select all

farm[\s\S]*?house|house[\s\S]*?farm
Thank you very much, It works great :appl: .
But I wonder if there is any simpler way to do this, Because sometimes I have to search for more than 2 words, lets say 6 words.
It will be tough mission to write this RegEX.
any script?
Thanks in advance
Last edited by karimmaster101 on 18 Feb 2014 16:07, edited 1 time in total.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Help with find files contents by RegEx, AND function

Post by TheQwerty »

highend wrote:

Code: Select all

farm[\s\S]*?house|house[\s\S]*?farm
Just curious but why the character class for white-space or not white-space instead of just matching anything?

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Help with find files contents by RegEx, AND function

Post by Marco »

That's to match terms not on the same line. The dot matches everything except \r\n, while [\s\S] matches newlines too.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Help with find files contents by RegEx, AND function

Post by TheQwerty »

Marco wrote:That's to match terms not on the same line. The dot matches everything except \r\n, while [\s\S] matches newlines too.
Ah! That makes sense... I didn't realize XY's dot didn't include line breaks. :oops:
Thanks!



As for karimmaster101's question...
I don't think XY's search is well-suited for what you're attempting. You might be better served looking for another program for this task. I don't have a suggestion but someone else here might.


That said, if you want to stick with XY then I'd suggest using a tags and multiple searches approach.

Say we want to search for files containing 'farm', 'house', and 'blues' in no particular order.
  1. Perform the search for one word - 'farm'.
  2. Select all results.
  3. In the menu select Favorites > Add Tags.
  4. In the Add Tags dialog create a unique tag for these items - 'contains:farm'.
  5. Now in the Find Files > Tags tab, enable Tags and in the corresponding field put that unique tag 'contains:farm'. (If in the previous step you checked Add new tags to tag list you can use the Select Tags... button here.)
  6. Now change the Contents tab to the next word 'house' and search again.
  7. Tag the results 'contains:house'.
  8. Update the Tags tab to search 'contains:farm , contains:house' (',' is logical 'AND' so this means items tagged with both. '|' is 'OR' and would be items tagged with either.)
  9. Now search for items containing 'blues' to arrive at the final results.
You might be able to speed up the process if you can make an educated guess and search for the words in order of least to most-frequently used.

Once you're done you can find all the tagged items by searching for items tagged 'contains:farm | contains:house' and use Favorites > Remove Tags by List or Remove All Tags to remove the tags. But if you do this task frequently it might make more sense to make the tags more permanent and occasionally perform unrestricted searches just to add tags to items containing your search words. Doing so could allow you to restrict the search to tagged items from the beginning.


It is possible to automate this task further with scripting, but neither approach I can think of is nice and simple. One way would be to bypass XY's search entirely and have the script read every file looking for the words, but this will be slower. The other requires creating/modifying a search template INI file to perform the searches with the LoadSearch scripting command - not difficult but not ideal either - and making it flexible becomes a challenge.


Sorry that this isn't a quick solution, or what you were likely hoping for, but hopefully it still helps! :)

highend
Posts: 14567
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Help with find files contents by RegEx, AND function

Post by highend »

The best solution would be e.g.: http://www.mythicsoft.com/agentransack

1. It's free
2. It allows you to use boolean AND OR
3. It accepts command line parameters so you can easily invoke it from XY with what's needed

One disadvantage: It seems it's not able to use boolean regex expressions (the commercial version does) so it finds e.g.
household and farmboy as well.

But there are so many alternatives that could be used (grep | gawk), I'm sure you'll find something...
One of my scripts helped you out? Please donate via Paypal

karimmaster101
Posts: 158
Joined: 20 May 2011 14:34

Re: Help with find files contents by RegEx, AND function

Post by karimmaster101 »

highend wrote:The best solution would be e.g.: http://www.mythicsoft.com/agentransack

1. It's free
2. It allows you to use boolean AND OR
3. It accepts command line parameters so you can easily invoke it from XY with what's needed

One disadvantage: It seems it's not able to use boolean regex expressions (the commercial version does) so it finds e.g.
household and farmboy as well.

But there are so many alternatives that could be used (grep | gawk), I'm sure you'll find something...
Thank you MILLION TIMES :appl: :appl:
It is a Great Program, That is what I am looking for. But I am still having a question :D
I created a button to open the program from XY, Here its script

Code: Select all

run '"C:\Program Files\Mythicsoft\FileLocator Pro\FileLocatorPro.exe"-d "(Desired Look in Path)"';
Now I want a way to make (look in path) be the current path which I explore inside XY.
I have tried <curpath> but apparently it just works inside xyplorer.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Help with find files contents by RegEx, AND function

Post by TheQwerty »

Single-quote strings are treated as literals meaning no variables are resolved, so you'll need to use double quotes instead.

Not knowing FLP but I'd assume this should work:

Code: Select all

run """C:\Program Files\Mythicsoft\FileLocator Pro\FileLocatorPro.exe"" -d ""<curpath>""";
For more information:
Don's quick set of rules to understanding quoting.
And my own much more detailed explanation.

karimmaster101
Posts: 158
Joined: 20 May 2011 14:34

Re: Help with find files contents by RegEx, AND function

Post by karimmaster101 »

TheQwerty wrote:Single-quote strings are treated as literals meaning no variables are resolved, so you'll need to use double quotes instead.

Not knowing FLP but I'd assume this should work:

Code: Select all

run """C:\Program Files\Mythicsoft\FileLocator Pro\FileLocatorPro.exe"" -d ""<curpath>""";
For more information:
Don's quick set of rules to understanding quoting.
And my own much more detailed explanation.
Thank you very much, it works now :biggrin:

Post Reply