5

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.

1
  • 3
    Notepad++ allows regular expression to find match [ use CTRL+F ]. You can write regular expression to match everything after face space character and replace it with nothing.
    – mnmnc
    Dec 17, 2012 at 11:02

4 Answers 4

8

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

7

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.

2

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

1
  • I am not good with programming languages sir,so something else means any program that i can download and use to do what i need to be done
    – Habeeb
    Dec 17, 2012 at 10:51
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .