How to keep only the first word in a line using Notepad++

I would like to keep only the first word of each line within Notepad ++. For an example, please consider the situation where I have these lines

aleag greats broa
abro gdert gfherr httowpw
asteruso dgheur graterf

Want I would like have left is this
aleag
abro
asteruso

How do I do this? Either using notepad++ or something else.


Solution 1:

I've only just looked at a reference sheet, so there's probably a better way to do this, but you could do something like this with the Replace dialog (Ctrl+H):

Find: ([0-9a-zA-Z]*)([0-9a-zA-Z ]*$)
Replace: \1

The way I read it is basically:
[0-9a-zA-Z] : match an alphanumeric character
* : keep matching them
[0-9a-zA-Z ] : match an alphanumeric character or a space
$ : match the end of the line
() : "holds" a match in a variable. The first () holds a match in \1, the second in \2 (which we don't need).

Since the first () is "holding" the match you want, that will be what you are replacing the text with. It does this for each line if you hit Replace All.

wordpad++ find replace

Solution 2:

A similar solution to the one Louis suggested is to again use Regular expression search within Notepad++, with the search parameter:

(^.+?)\s.*$

And replace with:

\1

The advantage/difference of this version over Louis' one is that it will also match first words like Apostrophy's or Colons: or Hyphen-ated where there are non alphanumeric characters.

This matches everything from the start of each line (the ^ symbol means start of line). It finds at least one character (.+? is non-greedy match one or more characters) stopping as soon as it finds a blank (\s is blank/space). It then carries on matching everything else in the line (.* is match zero or more characters), up to the end of the line ($ is end of line).

The \1 will then only keep the section matched within the brackets, I.E. everything before the first space in the line.

N.B. make sure the . matches newline checkbox is unticked, or you will only get the first word in the whole document.

Solution 3:

As you don't know any languages to program in, you can do this in Excel

Paste each line into Column A.

Then, in column B, paste

=LEFT(A1,SEARCH(" ",A1)-1)

enter image description here

Solution 4:

An even simpler solution is to use .*

Just open Notepad++, Choose regular expression's in search and replace, enter the below and then click replace all:

Find:  .*

Note that, the is a space in front of the dot and that replace is blank.

enter image description here