Tuesday, June 28, 2011

How can Visual Studio search and replace be so utterly broken ?

I have the thankless task of editing a ton of HTML files. I have a bunch of URLs that all have different IDs, but I want to point to the same place. So, I investigated regex searching in VS. Well, the regex was easy, and the search found all my strings. Replace, however, leaves the regex section behind. My search was s=c&vl=vlg&vi=\d*, and it would replace s=c&vl=vlg&vi= and leave my digits behind.

Some googling told me that VS has it's own Regex syntax ( although using standard syntax WAS matching my strings ), so I tried that. Still no dice, I get matches, but it won't replace the bit matched by regex. I am unsure what the point of that is.

So, I decided to try wildcards. s=c&vl=vlg&vi=#* works perfectly, it searches AND replaces. So, I showed it to friends I'd been asking for help with the regex. Some guy says 'but you always want there to be a number, * is zero or more'. I KNOW I was reading MSDN, so I grab the entry to prove him wrong. Here is the text from MSDN:

One or more characters * Matches zero or more characters. For example, new* matches any text that includes "new", such as newfile.txt.

So, is it one or more, or zero or more ? It's one or more. But, I had to test, because the documentation contradicts itself. How very helpful.

1 comment:

  1. http://msdn.microsoft.com/en-us/library/2k3te2cs(v=VS.100).aspx

    Funny, you're using the "#" operator (which is a non-greedy, "one or more" search) to match the "=" character, immediately followed by the "*" to match the preceding expression zero or more times, greedily. I *think* what you want is:

    (s=c&vl=vlg&vi=)(:d)+

    ..which can also be written:

    s=c&vl=vlg&vi=:d+


    It's not the documentation that contradicts itself. You've read it wrong, because otherwise you wouldn't be using "#" to match *digits*. Also, your understanding of what an expression is wrong, since you mistake a bunch of characters in a row with an expression. If you want a string to be interpreted as an expression, you have to group them with parentheses. So for example, the difference between searching for "me^2" and "(me)^2" is that the first would match "mee" in the word "meet" whereas the second wouldn't (it would match "meme" in the word "memes", though).

    ReplyDelete