Regular Expressions in Visual Studio - Turning A Querystring into a Class

This post will help you create a class that can be used in MVC Framework model binding, but is also a good demonstration of the Regular Expression support in Visual Studio’s Find & Replace feature.  The class created can be used with PayPal’s IPN notifications.

I just started working with the PayPal “Instant Payment Notification” API again. I wanted to use the model binding capabilities of Asp.Net MVC, so the first task was to extract the querystring variables and make properties out of them.  The sample querystring from the PayPay documentation for IPN is as follows:

mc_gross=19.95&protection_eligibility=Eligible&address_status=confirmed&payer_id=LPLWNMTBWMFAY
&tax=0.00&address_street=1+Main+St&payment_date=20%3A12%3A59+Jan+13%2C+2009+PST&payment_status=Completed
&charset=windows-1252&address_zip=95131&first_name=Test&mc_fee=0.88&address_country_code=US&address_name=Test+User
&notify_version=2.6&custom=&payer_status=verified&address_country=United+States&address_city=San+Jose
&quantity=1&verify_sign=AtkOfCXbDm2hu0ZELryHFjY-Vb7PAUvS6nMXgysbElEn9v-1XcmSoGtf&payer_email=gpmac_1231902590_per%40paypal.com
&txn_id=61E67681CH3238416&payment_type=instant&last_name=User&address_state=CA&receiver_email=gpmac_1231902686_biz%40paypal.com
&payment_fee=0.88&receiver_id=S8XGHLYDW9T3S&txn_type=express_checkout&item_name=&mc_currency=USD&item_number=
&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=&payment_gross=19.95&shipping=0.00

As you can see, it’s a bit of a mess.  It’s all on one line (if you copy and paste their sample) and you will need to break out the key-value pairs. This is made especially easy in Visual Studio, if you know a few tricks.

Start by adding a blank Text Document to your project and then paste in the IPN querystring provided by PayPal.  Here are the steps to build the class:

  1. Press CTRL + H to invoke the replace dialog. The first replace operation is to search for & and to replace with \n. Make sure you have “Use Regular Expressions” enabled in the options pull down.  We’re simply searching for an ampersand character and replacing it with a new line.
    image
  2. Press the button for Replace All and it’ll break everything on to separate lines for you.
  3. Next, search for =.* and replace it with nothing. This blanks out everything from the = sign on.
  4. Finally, we need to capture the text on the entire line. I had a couple of hiccups trying to get the capture working in Visual Studio, but this worked for me:
    1. Search for: ^{.*}$
    2. Replace with: public string \1 { get; set; }
      image

 

^ is a character for “start of line” and $ is the character representing “end of line”. The {.*} bits in between captures everything else on the line into a numbered group. Captured groups are numbered and start at 1.  In our replace statement, we’re basically writing out properties and using the \1 to insert the property name that we captured in the curly braces.

Note: at the time of this writing, I had to use VS2010 to do this last operation because VS11 Beta is not really giving the regex a lot of love.

When you’ve completed all those bits, you’ll have a bunch of properties ready to paste into a class:

image

If you’re interested, this is the class you need as an action parameter in an Asp.Net MVC project to capture the querystring parameters provided by the PayPal Instant Payment Notification (or IPN):

    public class PayPalIpnRequest
{
public string mc_gross { get; set; }
public string protection_eligibility { get; set; }
public string address_status { get; set; }
public string payer_id { get; set; }
public string tax { get; set; }
public string address_street { get; set; }
public string payment_date { get; set; }
public string payment_status { get; set; }
public string address_zip { get; set; }
public string first_name { get; set; }
public string mc_fee { get; set; }
public string address_country_code { get; set; }
public string address_name { get; set; }
public string notify_version { get; set; }
public string custom { get; set; }
public string payer_status { get; set; }
public string address_country { get; set; }
public string address_city { get; set; }
public string quantity { get; set; }
public string verify_sign { get; set; }
public string payer_email { get; set; }
public string txn_id { get; set; }
public string payment_type { get; set; }
public string last_name { get; set; }
public string address_state { get; set; }
public string receiver_email { get; set; }
public string payment_fee { get; set; }
public string receiver_id { get; set; }
public string txn_type { get; set; }
public string item_name { get; set; }
public string mc_currency { get; set; }
public string item_number { get; set; }
public string residence_country { get; set; }
public string test_ipn { get; set; }
public string handling_amount { get; set; }
public string transaction_subject { get; set; }
public string payment_gross { get; set; }
public string shipping { get; set; }

}</pre>