Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to use regular expression to surround my text with double quotes

The following is just a small scale example. How do I use search and replace (regular expression option) to surround each line with double quotes in Visual Studio 2012 search and replace?

List Item
    Item 1
    Item 2
End List Item

"List Item"
    "Item 1"
    "Item 2"
"End List Item"
like image 470
Rod Avatar asked Oct 16 '25 18:10

Rod


1 Answers

I agree, VS find & replace regexes are pretty confusing.

Let's start. We need to surround the text of each line with quotes, with leading spaces outside of the quotes.

So we need to create a first group that will catch the leading whitespaces and tabs, and a second one that will catch every word and space until the end of the line, and that will catch nothing if the line is empty.

Then we will replace by the first group, the quote char, the second group, and another quote char.

Regex translation:

Find: ([\t ]*)([\w ]+)

Replace by: $1"$2"

like image 127
Antoine C. Avatar answered Oct 18 '25 12:10

Antoine C.