Find and Replace Using Regular Expressions in Visual Studio 2013 and SQL Server Management Studio 2014

In Visual Studio, I had to replace a bunch of DropDownList ListItem states and countries from something like

<asp:ListItem id=”” value=”CO” Text=”Colorado” />

to

<asp:ListItem ID=”CO” value=”CO” Text=”Colorado” />

After dorking with it for a while, I found that this works:

Edit > Find and Replace

Find what:

id=”” value=”([A-Z][A-Z])”

Replace with:

ID=”$1″ value=”$1″

Be sure to check the Use Regular Expressions box.

Apparently, $1 uses the parenthesis in ([A-Z][A-Z]) to handle the replacement.

————————–

I also wanted to apply a class to the second cell in a table spanning multiple lines tab formatting in VS.

<- tab -><- tab -><- tab -><- tab -><- tab -></td><- CRLF ->

<- tab -><- tab -><- tab -><- tab -><- tab -><td class=”alignleft”>

Find what:

(?s)</td>([\s\n])*<td>

Replace with:

</td>\n\t\t\t\t\t<td class=”alignleft”>

It’s messy with the tab formatting in VS, but it works.

————————–

And take a data model like this:

public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }

and convert it into form fields like this:

<tr><td>Name</td><td class=”alignleft”><asp:TextBox ID=”txtName” Width=”230px” runat=”server”></asp:TextBox></td></tr>
<tr><td>Email</td><td class=”alignleft”><asp:TextBox ID=”txtEmail” Width=”230px” runat=”server”></asp:TextBox></td></tr>
<tr><td>Phone</td><td class=”alignleft”><asp:TextBox ID=”txtPhone” Width=”230px” runat=”server”></asp:TextBox></td></tr>

Find what:

public .* (.*) { get; set; }

Replace with:

<tr><td>$1</td><td class=”alignleft”><asp:TextBox ID=”txt$1″ Width=”230px” runat=”server”></asp:TextBox></td></tr>

————————–

SQL Server Management Studio does it a little differently:

for:

Search:

public {<.*>} {<.*>}

Replace:

\2 \1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s