All appearances in search
-
Hi,
I couldn’t find any answers for my problem, so sorry if already exists.
Notepad++ search the given criteria, next criteria starts searching from the end of the first one.
Ex: Text: aaaa
If you search for “aa” you will get 2 matches. (aaaa) and (aaaa).I need a way to search and count all appearances of “aa”, which means 3 matches. (aaaa), (aaaa) and (aaaa)
Thank you in advance!
-
Hello Radu Vîrşescu,
Yes, indeed ! Moreover, it will allow me to talk about the main property of a powerful feature of regular expressions : the look-arounds :-))
A look-around can be, either, a look-behind or a look-ahead and, either, positive or negative. A look-around contains its own regex ( or a string or a character ! ) which must be matched, at cursor location, in order that the regex engine considers that there a reel match of the entire regex, when the main regex is already matched
An example : let’s suppose the simple subject string abcdefghidefxyz, beginning a line and the regex
(def(?=ghi)|.)
-
When the position of the regex engine is located before the letters a, b or c, the regex engine chose the second part (
.
), of the alternative|
, because it can’t see any string def, at cursor location -
Then, the regex engine matches the string def, because that string is quite followed with the string ghi ( first part of the alternative
(def(?=ghi)
). It’s important to note that, although the regex engine needs to look till position 9, to verify that the string ghi is, really, present, the “official” location of the regex engine is, still, just after the string def and before the letter g ( so between columns 6 and 7 )
Indeed, if you go on searching, you, now, get, successively, the letters g, h, i, then, again, the letters d, e and f and, finally, the letters x, y and z, which are, all, matched, because of the dot (
.
). Note that the second string def, although found by the regex engine, is NOT considered as a match, just because it’s NOT followed by the string ghi !
So, Radu, you, certainly, have found out how to build your regex, for your counting !!
Simply, search for the regex
a(?=a)
Et voilà !For instance, given the subject string
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
( 30 letters a ), a click on the Count button returns 29 matches. Quite logical :-)Now, if you choose the regex
a(?=aaaaaaaaaa)
, you get, only, 20 matchesAnd if you choose the regex
aaa(?=aaaaa)
, you’ll obtain 8 matches ( 8 consecutive groups of the string aaa ), because the ninth and tenth groups of the string aaa are NOT followed by the exact string aaaaa !!Best Regards,
guy038
-