I am trying to use this regex:
^(\s+)<ProjectReference(.|\s)+?(Project2)</Name>(.|\s)+?</ProjectReference>
...to locate only this section:
<ProjectReference Include="..\..\Project2\Project2.csproj">
<Project>{6c2a7631-8b47-4ae9-a68f-f728666105b9}</Project>
<Name>Project2</Name>
</ProjectReference>
...in the below document:
what is causing this text up here to be selected??
<ProjectReference Include="..\..\Project1\Project1\Project1.csproj">
<Project>{714c6b26-c609-40a4-80a9-421bd842562d}</Project>
<Name>Project1</Name>
</ProjectReference>
<ItemGroup>
<ProjectReference Include="..\..\Project2\Project2.csproj">
<Project>{6c2a7631-8b47-4ae9-a68f-f728666105b9}</Project>
<Name>Project2</Name>
</ProjectReference>
<ProjectReference Include="..\..\Project3\Project3\Project3.csproj">
<Project>{39860208-8146-429f-a1d1-5f8ed2fd7f5f}</Project>
<Name>Project3</Name>
</ProjectReference>
<ProjectReference Include="..\..\Project4\Project4.csproj">
<Project>{58144d60-19d9-4d11-8ae6-088e03ccf874}</Project>
<Name>Project4</Name>
</ProjectReference>
<ProjectReference Include="..\..\Project5\Project5.csproj">
<Project>{33baa509-ad24-4a72-a2fc-8f297e75e90d}</Project>
<Name>Project5</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
In Notepad++, it appears to initially locate the match, but then it proceeds to match the entire document in a second match (so it's finding 2 matches total). I originally discovered this in my .NET app when my utility was replacing the entire contents of my project file with an empty string, effectively clearing the entire thing out.
I've spent over an hour toiling over this, so let's see if SE can figure it out.
Update: Though I've marked an answer that actually works, I ended up going with a not-so-magical approach to ensure that no rare regex quirks creep into my code later down the road as was the case recently.
^(\s+)<ProjectReference.+?({0})\.(csproj|vbproj).*\r\n.*\r\n\s+<Name>{0}</Name>\r\n\s*</ProjectReference>
...where {0}
is the name of my project. While more verbose, this solution is less likely to bug out with excessive matches. I use RegexOptions.Multiline
in my .NET app so that I can anchor to the beginning of a line.