99

I have quite a large list of words in a txt file and I'm trying to do a regex find and replace in Notepad++. I need to add a string before each line and after each line.. So that:

wordone
wordtwo
wordthree

become

able:"wordone"
able:"wordtwo"
able:"wordthree"

How can I do this?

2
  • Sorry those word strings are meant to be on seperate lines! I bet that has confused things now... Commented Nov 12, 2008 at 10:59
  • sweet save me a lot of googling :) Commented Jul 6, 2011 at 1:04

5 Answers 5

189

Assuming alphanumeric words, you can use:

Search  = ^([A-Za-z0-9]+)$
Replace = able:"\1"

Or, if you just want to highlight the lines and use "Replace All" & "In Selection" (with the same replace):

Search = ^(.+)$

^ points to the start of the line.
$ points to the end of the line.

\1 will be the source match within the parentheses.

Sign up to request clarification or add additional context in comments.

Comments

40

Regular Expression that can be used:

Find: \w.+
Replace: able:"$&"

As, $& will give you the string you search for.

Refer: regexr

4 Comments

Great mention of$&! The \1 doesn't work in all environments.
Great answer about $&. It works in SublimeText, too.
Great answer about $& . It works in VS code, too.
Great answer about $&. It works in PHPStorm, too, if you write it like $1.
31

Why don't you use the Notepad++ multiline editing capabilities?

Hold down Alt while selecting text (using your usual click-and-drag approach) to select text across multiple lines. This is sometimes also referred to as column editing.

You could place the cursor at the beginning of the file, Press (and hold) Alt, Shift and then just keep pressing the down-arrow or PageDown to select the lines that you want to prepend with some text :-) Easy. Multiline editing is a very useful feature of Notepad++. It's also possible in Visual Studio, in the same manner, and also in Eclipse by switching to Block Selection Mode by pressing Alt+Shift+A and then use mouse to select text across lines.

4 Comments

+1 This is really great, I had no idea you could do this. Is it possible to append something to the end of each line, when the length of each line is different? For example, when the 'column' width varies, it would be neat if you could append text to each line. I have a list of functions and I need to append "();" to each line but each function name is a different width. I am currently using a Macro to do this, thanks!
for appending is probably best to just use simple find/replace using the "Extended" search mode. You can search for line ending, e.g. \r\n and replace that with <whatever you want to append>\r\n
This is actually a really great solution, especially with the above APPEND comment.
This is useful for prefixing, but not so useful if you want to postfix mixed-length lines (append at the end). I wish Notepad++ has a built-in "Modify Lines" command like in Notepad2, which allows you to select the lines, press Alt+M, and add text before and/or after each line.
6

Use a Macro.

Macro>Start Recording

Use the keyboard to make your changes in a repeatable manner e.g.

home>type "able">end>down arrow>home

Then go back to the menu and click stop recording then run a macro multiple times.

That should do it and no regex based complications!

2 Comments

What is "complications" supposed to mean? :-)
+1 for ease of use! I needed to append a 'tab' to the end of each line in a file and this tip did the trick.
1

In visual studio code i found that simple regex as ^ worked.

2 Comments

How is this answering the question about Notepad++?
@Toto well, i personally was not googling the answer for Notepad neither right

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.