Not Operator – Additional Notes

Search & Replace regex2 shg Not Search Operator Notes

Here are some general notes about the Regular Expression ‘not’ operator, !.

  • ‘Not’ expressions can be thought of a being divided into two parts – an ‘expression to search for’ component and one or more ‘not’ component(s). The ‘! part’ always goes to the right of the expression to search for.

  • The ‘expression to search for’ component must include some type of regular expression search operator. This is because the ‘not’ component needs some kind of regular expression material to consider when evaluating whether the overall expression is satisfied.

  • To qualify the above, the ‘expression to search for’ part could be a non-regular expression term but if you search for ‘something’ but not ‘part of something’ you would end up with no hits. There is a further qualification to this… see below.

  • ‘Not’ components must be surrounded by paren characters. A common mistake can be seen with the ‘or’ operator, |. ‘or’ itself must be surrounded by paren’s. Therefore, if you use | in a not expression, double the paren’s. For example, cat*!(hat|dog) would not work. Use cat*!((hat|dog)) instead.

  • Consider the search expression, xref?!(=), where you want to find all instances of the string ‘xref’ except if followed by the = character. In words, this can be described as – “Find xref followed by anything. If the ‘anything’ that is returned by the ? operator is the = character, reject that search hit”.

  • It is OK to use multiple !( ) components. For example, cat*!(hat)!(dog) is also OK.

  • Consider the search expression, img src=”http://d2nwkt1g6n1fev.cloudfront.net/helpmax/wp-content/uploads/sub/searchandreplace/en/source/*.gif*!(images/)!(alt=”+[]“). In words, this means “find img scr=”, followed by anything, followed by .gif, followed by anything all the way to the end of the line. If either of the ‘anything’ text returned by the *’s contains the string ‘images/’ or alt=” ” that not empty, then reject the hit. This string would find <img src=”http://d2nwkt1g6n1fev.cloudfront.net/helpmax/wp-content/uploads/sub/searchandreplace/en/source/earth6.gif” alt=”"… but not <img src=”http://d2nwkt1g6n1fev.cloudfront.net/helpmax/wp-content/uploads/sub/searchandreplace/en/source/images/earth6.gif” alt=”Graphic pic”… or <img src=”http://d2nwkt1g6n1fev.cloudfront.net/helpmax/wp-content/uploads/sub/searchandreplace/en/source/earth6.gif” alt=” (1019 bytes)”…. Note that in the search expression, alt=”*[]” would not work. * is the ‘zero or more’ operator — it will always be true if any type of alt=” ” is present.

  • As modeled above, it is OK to use other regular expression search operators inside your ! operators. Another example: ^*!(^t)!(</p>$). In words, this means “find anything from the start of the line to the end of a ling, except if the first character on the line is the letter t or if </p> is at the very end of the line.

  • When using ! with replacements, don’t forget to specify the search expression literals in your replacement expression. For example, given the search expression ?at!((b|c)at) in a replacement to put quote characters around all strings ending in ‘at’ (except if they begin with ‘b’ or ‘c’), the replacement expression would be “%1at”. The ‘at’ string must be specified in the replacement expression because that is a literal in the search expression.

  • Null ! expressions can be employed as a type of “and” operator by specifying !() alone. For example, div*!()(center|right) will work. In words this means, “Find div followed by anything. If there is ‘not nothing’ in the * part, then check for (center or right). If either exist, then return the hit.

  • Another example: *void*!(end)!()printing . This expression finds ‘virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);’ but not ‘virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);’.

  • The components of the expression following the ! operator cannot be accessed via an %n replacement operator. For example, in the expression div*!()(center|right) above, there would be no %2 operator for use in a replace expression.

See ! – Not Operator for additional examples.

If you are transitioning from versions earlier than v 3.0, see Not Operator Changes and Old Syntax Examples for more information.

Not Search Operator Notes