Replacing last word in a line in Notepad++

notepad

How can I replace different last words in a line in Notepad++?

e.g.

This is line one
This is line two
This is line three
etc.

Replace with:

This is line /one/
This is line /two/
This is line /three/
etc.

Thanks.
Marco

Best Answer

If your goal is to surround the last word in a line with slashes, you can accomplish this task very easily by using regular expressions: In your Notepad++ window press Ctrl+F, choose the tab titled 'Replace' and select 'Regular Expression' search mode.

Use (\w+)$ as your search pattern in the 'Find what' field. Because of the parentheses Notepad++ will create a backreference meaning that the matched string inside the parentheses will be stored for further use.

  1. \w matches any word character (alphanumeric + underscore)
  2. + is a greedy quantifier and matches the previous item once or more, as many times as possible.
  3. $ Matches at the end of the string the regex pattern is applied to, in this case before line breaks.

In your case you'd use the following pattern in your 'Replace with' field: /\1/. \1 references the stored match from your first parentheses.

A good place to start with regular expressions is Regular-Expressions.info. You can try your regular expressions at RegExr.

Related Question