RegEx Expression to count a WORD in LINES

  • If I have 1000 lines of text, and a word MILKis found 1100 times in total, but the word MILK is not in all lines, because it is found several times in other lines.

    Is there a RegEx Expression to count the amount of lines in which the word MILK appears?

  • @Elijah-5801

    Sure, try: (?-s)^.*?MILK.*?\R

    Press the Count button.

    Make sure the last line has a line-ending on it if your word appears on that line and you want it counted. Sure would be nice to have a native option to ALWAYS put a line-ending on that last line!

  • Hi, @elija-5801 and All,

    Very easy to achieve, indeed ! Let’s suppose we have the following text and you would like to search for the word test :

    This is a test, a good test but a simple test
    A simple line
    The test to test
    A test to do
    Simple test which have not
    been tested, yet
    test test test
    To determine
    The number of lines
    • First, open a new tab ( Ctrl + N ) ( Leave the last line test without any line break )

    • Copy / paste, the text. Then,

    • Open the Find dialog ( Ctrl + F )

    • Select the Regular expression search mode

    • Type in the regex (?-s).*\btest\b, in order to know how many lines contain the word test, in the current file


    • Type in the regex \btest\b, in order to know the number of occurrences of the word test, in the current file

    • Click on the Count button

    => The result appears, at the bottom of the Find dialog

    Et voilà !


    P.S. :

    • Note that if we have searched for the string test, even glued in words, with the regexes :

    SEARCH (?-s).*test

    SEARCH test

    We would have obtained one more line and one more occurrence, because of the line “been tested, yet” !

    • See also my reply to your recent post :

  • I’m so appreciate this forum, the pros have solved two RegEx problems for me this weekend.

    Thanks to Scott and Guy038.

    Interestingly both your methods works perfectly.

Log in to reply