Search with content exclusion
-
I am a casual user of N++ and, definitely, not familiar with all of its functionality.
One way I would like to use N++ is search for patterns, but excluding sub-results.
An example would be to look for, let’s say, a “RESULTVALUE=” in a logfile, but wanting to ONLY see non-zero results.
In grep this would look something like:
grep “RESULTVALUE=” | grep -v “RESULTVALUE=0”
Is this possible in N++ and, if so, how? -
Hi @Peter-Cohen
Sure is possible. In order to accomplish your wish,
search
ormark
with the following regular expressiónRESULTVALUE=(?=")
Put the caret at the very beginning of the document, select the
Regular Expression mode
and click onFind Next
,Find All in Current Document
orMark All
as appropriated.Take care and have fun!
-
Hello astrofista,
Thanks for putting me on the path to a solution.
However, using your expression, I did not get the result I want.
Using the table below, I ONLY want to find line 2 & 4.
Using RESULTVALUE=(?=") gets me nothing.
Using RESULTVALUE=(?=0) gets me all the zero (0) values.
Is there a way to specify (?=Not 0)RESULTVALUE=0
RESULTVALUE=5
RESULTVALUE=0
RESULTVALUE=7
RESULTVALUE=0 -
For “not zero”, try
(?!0)
, soRESULTVALUE=(?!0)
for your need. -
Hi @Peter-Cohen
You’re welcome. My regex says: match every
RESULTVALUE=
that is followed by a quote"
, as I read your example. However it seems that I misunderstood your needs.Try this other regex instead:
RESULTVALUE=(?!0")
It will match every ‘RESULTVALUE=’ that is not followed by
0"
.Hope it’s fine now.
-
I really don’t think that
"
enter into the OP’s need at all.
He was just using the quotes because he’s familiar with doing it when using grep, I think. -
-
@astrosofista said in Search with content exclusion:
Remove the quote from my regex if not needed.
At which point, doesn’t it become the same as MY regex? ;-)
-
Gentlemen,
Thank you very much, RESULTVALUE=(?!0) did the trick.
Sorry if I confused you - I was even more confused.
This is definitely very powerful and useful (and tricky). -
BTW - would it be impertinent from me to ask WHY/HOW this works?
I am trying to decipher (?!0) and - admittedly - don’t understand what it actually stands for.
Interestingly, I changed one of the lines to:
RESULTVALUE=090 and now the search doesn’t find it, I suspect because it starts with a 0. -
@Peter-Cohen said in Search with content exclusion:
would it be impertinent from me to ask WHY/HOW this works?
Of course not.
The construct
(?!
…)
is a negative lookahead assertion.
It means that at the current position, for there to be a composite match, whatever...
is, cannot be a match for the data (or the overall match fails).Traditionally, in computer science,
!
means “not”.So in an Englishy way, the expression
RESULTVALUE=(?!0)
means:match
RESULTVALUE=
literally, and then insist that the next character is not0
for a match to be possible.But the way
(?!0)
is used here, it precludes090
from matching (the search stops trying to match at the first 0).If something is unclear, post again.
You can find many more details about the concepts begun here, in THIS thread.
-
Hello @Alan-Kilborn - going through a crash course here.
BTW - I used to be a computer programmer in a previous life, but this, definitely, is new territory for me :-|
I will definitely have to read up on this - thanks for the link. -
@Peter-Cohen said in Search with content exclusion:
RESULTVALUE=090 and now the search doesn’t find it, I suspect because it starts with a 0.
You got it, that’s correct.
However that doesn’t mean that you cannot match a number that starts with a 0. The following regex will match
RESULTVALUE=090
but notRESULTVALUE=0
orRESULTVALUE="
, that is, whenRESULTVALUE=
is followed by any other symbol than a number:RESULTVALUE=(?!0\D)(?=\d)
Hope this helps.
-
Holy cow - let me play with this - thanks.
-
Hi, @peter-cohen, @astrosofista, @alan-kilborn and All,
Let’s imagine that we want to search for any integer or floating decimal number, different of any zero syntax, when it follows the
RESULTVALUE=
string, with this exact case. Then a possible regex, expressed with the free-spacing mode ((?x)
) could be :(?x-i) RESULTVALUE= \K [+-]? ( (?! 0 \. 0? \D | 0 [^\d.] ) \d+ ( \. \d* )? | \. (?! 0 \D ) \d+ )
- Paste the text, below, in a N++ new tab
RESULTVALUE= No ResultValue= No RESULTVALUE=. No ResultValue=. No . No RESULTVALUE=, No ResultValue=, No , No RESULTVALUE=ABC No ResultValue=ABC No ABC No RESULTVALUE=0 No ResultValue=0 No 0 No RESULTVALUE=-0 No ResultValue=-0 No -0 No RESULTVALUE=+0 No ResultValue=+0 No +0 No RESULTVALUE=.0 No ResultValue=.0 No .0 No RESULTVALUE=-.0 No ResultValue=-.0 No -.0 No RESULTVALUE=+.0 No ResultValue=+.0 No +.0 No RESULTVALUE=0. No ResultValue=0. No 0. No RESULTVALUE=+0. No ResultValue=+0. No +0. No RESULTVALUE=-0. No ResultValue=-0. No -0. No RESULTVALUE=0.0 No ResultValue=0.0 No 0.0 No RESULTVALUE=+0.0 No ResultValue=+0.0 No +0.0 No RESULTVALUE=-0.0 No ResultValue=-0.0 No -0.0 No RESULTVALUE=0.00120 Yes ResultValue=0.00120 No / Yes 0.00120 No RESULTVALUE=0.00123 Yes ResultValue=0.00123 No / Yes 0.00123 No RESULTVALUE=0.12300 Yes ResultValue=0.12300 No / Yes 0.12300 No RESULTVALUE=0.42 Yes ResultValue=0.42 No / Yes 0.42 No RESULTVALUE=.71 Yes ResultValue=.71 No / Yes .71 No RESULTVALUE=2 Yes ResultValue=2 No / Yes 2 No RESULTVALUE=5. Yes ResultValue=5. No / Yes 5. No RESULTVALUE=6.8 Yes ResultValue=6.8 No / Yes 6.8 No RESULTVALUE=007.1 Yes ResultValue=007.1 No / Yes 007.1 No RESULTVALUE=091 Yes ResultValue=091 No / Yes 091 No RESULTVALUE=+0.00120 Yes ResultValue=+0.00120 No / Yes +0.00120 No RESULTVALUE=+0.00123 Yes ResultValue=+0.00123 No / Yes +0.00123 No RESULTVALUE=+0.12300 Yes ResultValue=+0.12300 No / Yes +0.12300 No RESULTVALUE=+0.42 Yes ResultValue=+0.42 No / Yes +0.42 No RESULTVALUE=+.71 Yes ResultValue=+.71 No / Yes +.71 No RESULTVALUE=+2 Yes ResultValue=+2 No / Yes +2 No RESULTVALUE=+5. Yes ResultValue=+5. No / Yes +5. No RESULTVALUE=+6.8 Yes ResultValue=+6.8 No / Yes +6.8 No RESULTVALUE=+007.1 Yes ResultValue=+007.1 No / Yes +007.1 No RESULTVALUE=+091 Yes ResultValue=+091 No / Yes +091 No RESULTVALUE=-0.00120 Yes ResultValue=-0.00120 No / Yes -0.00120 No RESULTVALUE=-0.00123 Yes ResultValue=-0.00123 No / Yes -0.00123 No RESULTVALUE=-0.12300 Yes ResultValue=-0.12300 No / Yes -0.12300 No RESULTVALUE=-0.42 Yes ResultValue=-0.42 No / Yes -0.42 No RESULTVALUE=-.71 Yes ResultValue=-.71 No / Yes -.71 No RESULTVALUE=-2 Yes ResultValue=-2 No / Yes -2 No RESULTVALUE=-5. Yes ResultValue=-5. No / Yes -5. No RESULTVALUE=-6.8 Yes ResultValue=-6.8 No / Yes -6.8 No RESULTVALUE=-007.1 Yes ResultValue=-007.1 No / Yes -007.1 No RESULTVALUE=-091 Yes ResultValue=-091 No / Yes -091 No
-
Open the Mark dialog (
Ctrl + M
)-
SEARCH
(?-i)RESULTVALUE=\K[+-]?((?!0\.0?\D|0[^\d.])\d+(\.\d*)?|\.(?!0\D)\d+)
-
Tick the
Purge for each search
option -
Tick the
Wrap around
option -
Select the
Regular expression
search mode -
Click on the
Mark All
button
-
=> Only numbers, located after the
RESULTVALUE=
string, with this case, and strictly different from0
, should be highlighted inred
Now, if you modify the regex as
(?i)RESULTVALUE=\K[+-]?((?!0\.0?\D|0[^\d.])\d+(\.\d*)?|\.(?!0\D)\d+)
, the numbers, located after the stringResultValue=
are also highlighted !
Peter, this example is simply provided to show the powerful of regular expressions. Of course, you cannot fully understand that example, by now. Just execute the regexes to get the results and, from the @alan-kilborn’s link, try to learn the basic regex features !
Best Regards,
guy038
-
Good afternoon @Folks,
I am off to regex school, although, admittedly, these last expressions do make my head spin @Exorcist :-)