Find multiple words in one line using Notepad++

2019-09-09 02:17发布

问题:

Below is my function to find the combination of two words in VS or Notepad++.

Find: Word1+[ \w\S]*(?=Word2)

Problem - Change the order of words to search for.

For example:

(function ( a, b, c, myVars) {})() text for search

function+[ \w\S]*(?=myVars) will be found.

myVars+[ \w\S]*(?=function) not will be found.

Need: Word1 <---> Word2

I will be glad to see the answer.

More examples:

html file split lines:       find:     (<),      replace:       \n$0

html join tag with name:     find:     \n</name>      replace:      </name>

wrap in tag:     find:    ^([ \w\S]+)    replace:      <tag>\1</tag>

HTML Character Codes      <  -  &#x3c;       and  >  -  &#x3e;

回答1:

I'm not using Notepad++, I'm using UltraEdit and therefore the regular expression below was tested only with Perl regular expression engine of UltraEdit.

\<(word1|word2)\>(?:[\t \S](?!\<\1\>))*?\<(?:word1|word2)\>

This regular expression searches first for either word1 OR word2 which must be found as complete word and not as substring within a word as \< means beginning of a word and \> means end of a word. It would be also possible to use \b (word boundary) instead of \< and \>.

If one of the 2 words is found in a line, it searches next for 0 or more characters being either a space character or any non whitespace character non greedy. It is also possible to use . instead of character class [\t \S] if the default matching behavior for a dot is any character except line terminators which is true for UltraEdit to limit the search on a line.

On every character of this string between the two words a negative lookahead is applied checking if the next characters after the already matched character are not equal the word found by the first OR expression. This additional condition avoids finding lines containing only one of the 2 words in the OR expression twice.

Last the OR expression from the beginning is added a second time to the expression, but this time as non marking group. Therefore the search must match now also the other word from the OR expression to return a positive result.