1

dir1\dir2\dir3\file.aspx.cs(343,49): error CS0839: Argument missing [C:\dir\dir\dir\dir\namespace.namespace.namespace.namespace\project.csproj]

I've been trying for hours to create a regular expression to extract just 2 areas of this string. The bold areas are the parts I wish to capture.

I need to split this string into two seperate strings:

  1. I'd like everything before the first "("
  2. everything between the "[" and "]" but not including the "project.csproj"

For #1 the closest i've gotten is (^.*\() which will basically capture everything up to the first "(" (including the bracket though which I don't want)

For #2 the closest i've gotten is (\[.*\]) which will basically capture everything inside the brackets (including the brackets which i don't want).

Any of the words in the above string could change apart from ".csproj" "C:\" and ".cs"

Context: This is how MSBuild spits errors out when compiling. By capturing these two parts I can concatenate them to provide an exact link to the erroring file and open the file in Visual Studio automatically:

System.Diagnostics.Process.Start("devenv.exe","/edit path");
4

2 回答 2

5

这种模式:

(^[^(]*).*\[(.*)project.csproj]$

捕获这些组:

  1. dir1\dir2\dir3\file.aspx.cs
  2. C:\dir\dir\dir\dir\namespace.namespace.namespace.namespace\

如果名称project.csproj文件可以更改,您可以改用此模式:

(^[^(]*).*\[(.*\\)[^\\]*]$

这将匹配所有内容,直到括号内的最后一个路径片段。

于 2013-06-27T19:07:56.680 回答
1

好吧,现在括号对你没有任何好处。只需将它们放在您感兴趣的部分周围:

^(.*)\(

\[(.*)\]

现在要从匹配中排除projects.csproj,只需将其包含在.*:

\[(.*)projects.csproj\]

然后match.Groups(1)会在每种情况下match为您提供所需的字符串(您的Match对象在哪里)。

如果projects.csproj可以是任何文件名(即您只想要最后一个反斜杠之前的所有内容,请使用:

\[(.*?)[^\\]*\]
于 2013-06-27T19:08:16.560 回答