• Login
Community
  • Login

Finding the Nth Word in a File

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 3 Posters 456 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.
  • K
    Kristopher Gill
    last edited by Sep 2, 2024, 6:44 PM

    OK, I am relatively new to Notepad++. I used to use it years ago for a lot of my programming, but it’s been ages since I’ve left the IT field. So please keep it Barney style for me.

    Can someone explain if it’s possible to use Notepad++ find/highlight a particular word in a document. I don’t want to find and replace. I simply want to find it. For example, let’s say I pasted as simple text this sentence: “The quick red fox jumped over the lazy brown dog.”

    I would like to enter somewhere find the 4th word, and have a result come back as “fox.”

    1. Is this possible? 2. Can it be done with any length of a document?

    Thanks in advance,
    Kris

    A 1 Reply Last reply Sep 2, 2024, 7:36 PM Reply Quote 0
    • A
      Alan Kilborn @Kristopher Gill
      last edited by Alan Kilborn Sep 2, 2024, 7:41 PM Sep 2, 2024, 7:36 PM

      @Kristopher-Gill

      Well, you can at least approximate it by searching in Regular expression mode for:

      \A\W*?(?:(\w+)(?:\W+|\z)){N}

      where N is desired word number.

      But there are a lot of caveats about what characters make up a word – in the expression I used they are A thru Z, a thru z, 0 thru 9, and underscore.

      And it will match the text from the beginning up to the word number of interest, not simply the word of interest. I’m sure it could be made fancier using \K in the expression to highlight only the word of interest, but then some math would have to be done, e.g. searching for N-1 instead of N.

      I won’t bother explaining how it works, since I don’t like talking to purple dinosaurs.

      K 1 Reply Last reply Sep 2, 2024, 8:33 PM Reply Quote 4
      • K
        Kristopher Gill @Alan Kilborn
        last edited by Sep 2, 2024, 8:33 PM

        @Alan-Kilborn Haha! Thank you! Now for something even more fun. Where do I plug in this expression to perform the search?

        Thanks again!

        A 1 Reply Last reply Sep 2, 2024, 8:48 PM Reply Quote 0
        • A
          Alan Kilborn @Kristopher Gill
          last edited by Sep 2, 2024, 8:48 PM

          @Kristopher-Gill said in Finding the Nth Word in a File:

          Where do I plug in this expression to perform the search?

          When I said:

          searching in Regular expression mode

          I meant: Go to the Search menu and select Find …. Then put the expression I gave in the Find what box, select Regular expression in the Search mode area, then press Find All in Current Document.

          K 1 Reply Last reply Sep 2, 2024, 11:51 PM Reply Quote 2
          • K
            Kristopher Gill @Alan Kilborn
            last edited by Sep 2, 2024, 11:51 PM

            @Alan-Kilborn Did exactly as I was looking for/intended. Thank you again for your help. This is great.

            1 Reply Last reply Reply Quote 0
            • G
              guy038
              last edited by guy038 Sep 4, 2024, 11:15 PM Sep 4, 2024, 10:08 PM

              Hello, @kristopher-gill, @alan-kilborn and all,

              Personally, I would use the following regexes, having the same concept of Nth word :

              • The regex \A\W*(?:\w+\W+){N-1}\K\w+ finds/marks the Nth word, of any file containing, at least, N words

              • The regex (\A|\R\h*\R)\W*(?:\w+\W+){N-1}\K\w+ finds/marks the Nth word, of any paragrah containing, at least, N words

              • The regex (?-s)^[^\w\r\n]*(?:\w+[^\w\r\n]+){N-1}\K\w+ finds/marks the Nth word, of any line containing, at least, N words


              Notes :

              • The Regular expression radio button is selected

              • The Wrap around option is checked

              • In case of a Find Next search, move to beginning of file, first ( Ctrl + Home )

              • Use, either, the Find Next, the Find All in Current Document, the Find All in All Opened Documents or the Mark All button

              Best Regards,

              guy038

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