开发者

Regular Expression to match a string, that doesn't end in a certain string

开发者 https://www.devze.com 2023-04-07 17:41 出处:网络
I an trying to write a regular expression to be used as part of a replace operation. I have many paths as follows:

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消