3

How do we achieve the following using replace function in notepad++?

Transforming the following

cow
horse
elephant
camel

into

'cow','horse','elephant','camel'

I know ^ and $ (under Regular Expression) will let me access the beginning and end of the line and therefore I can put the single quotes at the extremes but then how do I append the lines thereafter? Also, I want to do everything (putting quotes and appending) in one go if possible.

Thank you very much for your help.

2 Answers 2

3
  1. Download the latest version, it includes regular expression fixes.

  2. Replace using the regular expression: (.*)[\r\n]* and turn it into '\1',

  3. Remove the last ,, it's not possible to do this inside the regular expression in Notepad++.

5
  • Thanks @Tom . Will you mind explaining a bit on your second step...
    – Stat-R
    Apr 11, 2012 at 15:46
  • "fixes" link does not work. Apr 11, 2012 at 16:16
  • @Stat-R: I guess you figured out because of the replace, but it is basically like this: You go to the "replace" dialog and then tick "Regular Expression" and then fill in the two codes I gave you in the two boxes. Apr 11, 2012 at 17:17
  • @TomWijsman: "This page has been deleted"? Well, perhaps it shows that it is no longer an issue since the wiki page is no longer needed, if that is what you mean. Apr 12, 2012 at 6:22
  • @DanielAndersson: Now read the whole line under that. :D Apr 12, 2012 at 9:48
2

This solution is not a 100% percent solution, but it gets you most of the way there and is very simple. When I want to do this I use the "Extended" search mode on the replace dialog and use \r\n as the search term. In your case you would replace it with ','. That will get you to

cow','horse','elephant','camel

Now you just need to fix the first and last elements manually.

3
  • I tried this before asking my question here but for some reason (??) this did not work for me. I am using NP++(6.1).
    – Stat-R
    Apr 11, 2012 at 15:44
  • I just tried it on 6.1 and it worked fine. Make sure you file is using Windows line endings (View->Show Symbol->Show End of Line). You should see a CRLF at the end of each line. If you just see LF change your search term to \n.
    – heavyd
    Apr 11, 2012 at 16:00
  • I saw CR and I used \r. It worked. Thanks @heavyd
    – Stat-R
    Apr 11, 2012 at 16:22

You must log in to answer this question.

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