<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d8211560\x26blogName\x3dTech+Tips,+Tricks+%26+Trivia\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://mvark.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://mvark.blogspot.com/\x26vt\x3d-5147029996388199615', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

Tech Tips, Tricks & Trivia

by 'Anil' Radhakrishna
An architect's notes, experiments, discoveries and annotated bookmarks.

Search from over a hundred HOW TO articles, Tips and Tricks


HOW TO Find and Replace Characters Using Regular Expressions in Visual Studio

There was a question on ASP.NET Forums to find a pattern using regular expressions to match the following strings in code within Visual Studio -
href="ProgramInformation.aspx?ID=TX"
href="ProgramInformation.aspx?ID=NY"
so that they can be replaced with the following strings -
href="javascript:SetStateCode('TX')"
href="javascript:SetStateCode('NY')"

Most editors support regular expressions to find and replace characters. Visual Studio supports it as well but has it's own regular expression dialect.

Tagged expressions aid replacements in a regular expression search by extracting a specific set of characters that are required to be manipulated. In a single find/replace, you can have up to 9 tagged expressions. To tag our search criteria, we place {} around the criteria.

For the above requirement, we have to capture just the value of ID from the URL set to the HREF property and use it to build another string. To accomplish this, in the Find and Replace dialog box (Ctrl + H or Edit > Find and Replace > Quick Replace), place this in the "Find what:" box -
ProgramInformation.aspx?ID={[A-Z]+}

and this in the "Replace with:" box
javascript:SetStateCode('\1')

Click Replace or Replace All to change a single or multiple instances of the matching pattern.

Find and Replace Characters Using Regular Expressions in Visual Studio

Labels: ,

Tweet this | Google+ it | Share on FB

« Home | Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »

Posted by Anonymous Chon | 5/14/2009 07:05:00 PM

I have been wondering how to do this. I tried once and never got it to work, so I gave up. Thanks!    



»

Post a Comment