Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace assembly with regex in visual studio

We have a solution with over 25 projects, each one with it's own AssemblyInfo I am trying to increment all of the assembly numbers to increase one major version for our next release.

My regex skills are minimal but essentially I want to find:

<Assembly: AssemblyVersion("x.x.x.x")>

Where x in regex terms will be \d/a number. Looking here I thought I could use:

{AssemblyVersion("[\d].[\d].[\d].[\d]")}

Which didn't work, it doesn't like the open brackets. My question is essentially broken down into two stages:

  1. What regex string will I use to find the example?
  2. Is it possible to replace the first digit with the digit + 1 using regex? (I suspect I am being a bit optimisitic) if so what will the regex string be in this situation?

This is in source control, so I wouldn't like to modify the files directly.

Thanks in advance.

like image 294
LukeHennerley Avatar asked Mar 14 '13 15:03

LukeHennerley


People also ask

How do I find and replace in Visual Studio?

You can find and replace text in the Visual Studio editor by using Find and Replace (Ctrl+F or Ctrl+H) or Find/Replace in Files (Ctrl+Shift+F or Ctrl+Shift+H). You can also find and replace only some instances of a pattern by using multi-caret selection.

How do you write regex in Visual Studio code search?

Vscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.

Can I use regex in replace?

How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.


1 Answers

Note: this is using the Visual Studio regex find and replace.

I don't think you can add one while doing a regex replace. However, you could substitute a literal number. For example, you could use a regex that replaces the first digit 1 with a 99:

AssemblyVersion("1.0.2121.0") --> AssemblyVersion("99.0.2121.0")

Here is the regex I used for the Visual Studio Search and replace:

Search Regex:  AssemblyVersion[(]["]([0-9]+)([.][0-9]+[.][0-9]+[.][0-9]+)["][)]
Replace Regex: AssemblyVersion("99${2}")

The search regex escapes all special characters (.") by wrapping them in square brackets. Then it uses parenthesis to split the version number into two groups. The first group is the first set of digits. The second group is the last three sets of digits.

For example, AssemblyVersion("1.0.2121.0") is grouped like this (using braces for groupings): AssemblyVersion("{1}{.0.2121.0}"). So group 1 is 1, and group 2 is .0.2121.0.

The replace regex AssemblyVersion("99${2}")uses a literal and group 2 from the search regex to build the replacement string. The ${2} means use the text from group 2. The rest of the string is the literal.

like image 86
chue x Avatar answered Oct 23 '22 08:10

chue x