Add " before and after every line

A place to try and solve your RegEx problems.
Post Reply
User avatar
Animention

Add " before and after every line

Post by Animention »

Hi, I'd like to know how to insert " before and after every line of a txt file. So it becomes something like this:


Cookie
Bread
Milk

Becomes:

"Cookie"
"Bread"
"Milk"
:D
User avatar
Fool4UAnyway

Re: Add " before and after every line

Post by Fool4UAnyway »

The easiest way to do this is: "take everything on a line, and re-place it with a quotation mark before and after it".

Make sure you do NOT have the dot character match new lines characters.

Find:
(.*)

You may also explicitly specify the start and end of the line requirement:
^(.*)$

Both variant will also match empty lines. If you do not white space lines to be changed, you may use the following expression to explicitly exclude those:
([^\s]+)

Replace By:
"$1"

It won't be a problem for Text Crawler, but if you are inside an editor this kind of processing may be quite processor-intensive. An alternative way is to match only newline characters and re-place those surrouneded by quotation marks.

Find:
\r\n

Replace By:
"\r\n"

You will still have to edit the start of the first and the end of the last line when using this method.
User avatar
Fool4UAnyway

Re: Add " before and after every line

Post by Fool4UAnyway »

Both variant will also match empty lines. If you do not white space lines to be changed, you may use the following expression to explicitly exclude those:
([^\s]+)
This is not entirely true. This would only process lines that do not have any white space character at all...

This one requires (at least) one character that is not white space:
(.*[^\s].*)
User avatar
Fool4UAnyway

Re: Add " before and after every line

Post by Fool4UAnyway »

This one requires (at least) one character that is not white space:
(.*[^\s].*)
And again, this one is less processing intensive: it takes any white space characters, if present, then the one non-white space character and then just anything that follows.

Find:
([\s]*[^\s].*)
Post Reply