Community
    • Login

    RegEx Expression to count a WORD in LINES

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 5.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Elijah 5801E
      Elijah 5801
      last edited by

      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?

      Scott SumnerS 1 Reply Last reply Reply Quote 1
      • Scott SumnerS
        Scott Sumner @Elijah 5801
        last edited by Scott Sumner

        @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!

        1 Reply Last reply Reply Quote 0
        • guy038G
          guy038
          last edited by guy038

          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
          
          test
          
          A simple line
          The test to test
          A test to do
          test
          Simple test which have not
          been tested, yet
          
          test test test
          To determine
          The number of lines
          test
          
          • 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

          OR

          • 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à !

          guy038

          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 :

          https://notepad-plus-plus.org/community/topic/15390/regex-expression-help-with-keeping-text-and-replacing-formating-codes-only/4

          1 Reply Last reply Reply Quote 0
          • Elijah 5801E
            Elijah 5801
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors