.NN #14: RegEx Option for Find/Replace
This might seem more like a “Visual Studio Tip of the Day”, but today’s .NET Nugget is focusing on the Regular Expression option for the Find/Replace feature in Visual Studio. Sara even mentioned this way back on tip #75 (she’s on # 172 now). I ran into a situation the other day where this helped out a great deal and I thought I’d share.
NOTE: If you think find/replace functionality isn’t that interesting, at least skip to the final paragraph to read the real point behind this post.
I was creating an XML document that was going to be used as a message between systems. The DBA has shared with me the output of the data (aliased of course). Keep in mind that the actual output had about two dozen fields, each with a matching _Changed field to indicate that the value for the column had changed (we were publishing data notifications to the other system that our data had changed), but for now I’ll use the following as an example:
LASTNAME, FIRSTNAME, MIDDLENAME, ADDRESS1, ADDRESS2, CITY, STATE, ZIP
On the white board we had decided that we wanted to have the following structure to the XML:
<entity>
<lastname modFlag="0"></lastname>
<firstname modFalg="0"></firstname>
...
</entity>
I was asked to create a quick example of the message so we could pass it over the wall to the other team. I could have just copied the text into a file and started a lot of cut and pasting, but instead decided there was another way. All I had was the field list above. Here’s what I did:
- Created an XML file in Visual Studio
- Copied in the field list into the XML file under the XML declaration.
<?xml version="1.0" encoding="utf-8"?>
LASTNAME, FIRSTNAME, MIDDLENAME, ADDRESS1, ADDRESS2, CITY, STATE, ZIP
- Opened up the Find/Replace window (Ctrl-H on my keyboard scheme).
- Switched the options to use Regular Expressions and made sure the “Use” option was checked.
- I wanted to get each of the fields on their own line so I filled in the search Regular Expression with a comma and the replace expression with \n (newline).
- I clicked Replace all and now I had the following:
<?xml version="1.0" encoding="utf-8"?>
LASTNAME
FIRSTNAME
MIDDLENAME
ADDRESS1
ADDRESS2
CITY
STATE
ZIP
- I Highlighted all of the text and then used Shift-Tab to make it flush with the left margin. This just made sure I didn’t have to deal with spaces anywhere in my next regular expression.
<?xml version="1.0" encoding="utf-8"?>
LASTNAME
FIRSTNAME
MIDDLENAME
ADDRESS1
ADDRESS2
CITY
STATE
ZIP
- Next I selected just the field names, skipping the xml declaration.
- I wanted lowercase text so I used the lowercase command (Ctrl-U, or Edit -> Advanced -> Make Lowercase). Now I had:
<?xml version="1.0" encoding="utf-8"?>
lastname
firstname
middlename
address1
address2
city
state
zip
- Then I changed the regular expression to do the following:
This searched for the beginning of a line, followed by any number of characters, followed by the end of the line. It then replaced that with the text <\1 modFlag=“0”><\1> where \1 was the found expression within the curly braces. If you press the arrow next to the Replace With dropdown you’ll see the following:
Note that each “Tagged Expression” is what was captured within the curly braces so if we had multiple curly braces in our Find What text we could reference them in order. When you select one of the Tagged Expression options it simply puts a \1, \2, etc. into the text for the Replace With text.
Also note that I used the Selection “Look In” option so that I didn’t pick up the XML Declaration in this search.
- Clicking Replace All yielded me:
<?xml version="1.0" encoding="utf-8"?>
<lastname modFlag="0"></lastname>
<firstname modFlag="0"></firstname>
<middlename modFlag="0"></middlename>
<address1 modFlag="0"></address1>
<address2 modFlag="0"></address2>
<city modFlag="0"></city>
<state modFlag="0"></state>
<zip modFlag="0"></zip>
- All I needed to do was add the root entity tag, then used the format document command (Ctrl-K, Ctrl-D, or on the Edit -> Advanced -> Format Document menu) and my result was:
<?xml version="1.0" encoding="utf-8"?>
<entity>
<lastname modFlag="0"></lastname>
<firstname modFlag="0"></firstname>
<middlename modFlag="0"></middlename>
<address1 modFlag="0"></address1>
<address2 modFlag="0"></address2>
<city modFlag="0"></city>
<state modFlag="0"></state>
<zip modFlag="0"></zip>
</entity>
Remember that there were two dozen or so fields, so manually going to each one and writing this out, or cut/copy/pasting to the same result would have taken a little while. If you know what you’re doing you can pull off the above steps in less than two minutes, saving yourself a lot of time. We were able to get a trumped up copy of the message out in no time. The other group could have pressed forward without waiting on us and then we could focus on how to actually create our message through the system.
The point of this .NET Nugget is not to say that Regular Expressions are cool, or that Find/Replace is overly powerful, but rather it is important to learn and understand the tools you are working with. When you know the shortcuts in the tools you use on a daily basis you will increase your productivity and move you past the mundane tasks to focus on the really interesting ones. Sign up for Sara Ford’s VS Tip of Day. You may know a lot about Visual Studio, but I’ll bet she has a few things on there that you never knew you could do.