Regular Expression to match a string, that doesn't end in a certain string
I an trying to write a regular expression to be used as part of a replace operation.
I have many paths as follows:
开发者_如何学C<ProjectReference Include="..\Common.Workflow\Common.Workflow.csproj">
<ProjectReference Include="..\Common.Workflow\Common.Workflow.Interfaces\Common.Workflow.Interfaces.csproj">
<ProjectReference Include="..\Common.Workflow\Common.Workflow.Persistence\Common.Workflow.Persistence.csproj">
<ProjectReference Include="..\Common.Workflow\Common.Workflow.Process\Common.Workflow.Process.csproj">
I need to replace the Common.Workflow\
in all cases except where the it contains Common.Workflow.csproj
.
I am moving these files as part of a code clean up.
Replace Common\.Workflow\\(?!Common\.Workflow\.csproj)
with what you need.
Use a negative look-ahead and a negative look-behind, like this:
(?<!.*Common.Workflow.csproj)Common.Workflow\\(?!.*Common.Workflow.csproj)
Explanation:
(?<!.*Common.Workflow.csproj)
means "Common.Workflow.csproj
must not appear before the match(?!.*Common.Workflow.csproj)
means "Common.Workflow.csproj
must not appear after the match"
This regex will prevent matching if Common.Workflow.csproj
appears anywhere in the input (you didn't specify that the negative match only appears after the search)
精彩评论