DaveShaw
DaveShaw

Reputation: 52798

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:

<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.

Upvotes: 2

Views: 1060

Answers (2)

Bohemian
Bohemian

Reputation: 425033

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)

Upvotes: 2

rid
rid

Reputation: 63442

Replace Common\.Workflow\\(?!Common\.Workflow\.csproj) with what you need.

Upvotes: 4

Related Questions