Regex to Find Words Near Other Words
Posted: 30 Nov 2016 01:33
I got a little irritated the other day at the limitations of CHM format files. So, I unzipped the html, turned them into plain text, then combined and edited them, and then used this little regex (among others) to help find what I'm looking for. It's also useful, of course, finding things using XY either in content or in filenames with the search feature.
\bword1(?:\W+\w+){0,6}?\W+word2\b
Substitute the "6" for however many words you wish to constrain the forward search by, and substitute word1 and word2 exactly in those positions. If you wanted to add separate by at least so many words, change the "0".
(All hail to Jan Goyvaerts for this regex (and his tutorials, and his software). There's also a version of this regex where you use alternation to also find the words in reverse sequence, but this is the most useful to me, and the easiest to understand.)
***
The utilities I use to extract and combine CHM files, if you're interested, are 7-zip to unzip the CHM, then HTMLAsText from NirSoft (a very reliable company for free utilities - you should browse their site if you never have), and then Mr. Goyvaerts' (of Just Great Software) EditPad - either pro or lite (free, no strings) because of its beautiful regex support and a raft of other great features. Notepad++ would work too, ofc. One important regex to run, imho, is one that deletes duplicate lines as there tend to be a lot of them due to the html hierarchy. Again, EditPad works very well with such things.
PS
I wish there were a comprehensive PDF I could search through instead for XY's help.
\bword1(?:\W+\w+){0,6}?\W+word2\b
Substitute the "6" for however many words you wish to constrain the forward search by, and substitute word1 and word2 exactly in those positions. If you wanted to add separate by at least so many words, change the "0".
(All hail to Jan Goyvaerts for this regex (and his tutorials, and his software). There's also a version of this regex where you use alternation to also find the words in reverse sequence, but this is the most useful to me, and the easiest to understand.)
***
The utilities I use to extract and combine CHM files, if you're interested, are 7-zip to unzip the CHM, then HTMLAsText from NirSoft (a very reliable company for free utilities - you should browse their site if you never have), and then Mr. Goyvaerts' (of Just Great Software) EditPad - either pro or lite (free, no strings) because of its beautiful regex support and a raft of other great features. Notepad++ would work too, ofc. One important regex to run, imho, is one that deletes duplicate lines as there tend to be a lot of them due to the html hierarchy. Again, EditPad works very well with such things.
PS
I wish there were a comprehensive PDF I could search through instead for XY's help.