문제

Lets say I got this AB_1.2.3.112211 in a property

What I want is to exchange the first "." to a "_"

So it becomes AB_1_2.3.112211 The two first characters could be longer fx ABCD_1.2.3.112211 but it should still be the output ABCD_1_2.3.112211

Is there a way to do this in msbuild task?

도움이 되었습니까?

해결책

If the input strings are always as you show them, i.e. the first "." to replace comes after an underscore followed by a number you can use a property function and use C#'s static Regex.Replace method:

<Target Name="Repl">
  <PropertyGroup>
    <Prop>ABCD_1.2.3.112211</Prop>
  </PropertyGroup>
  <Message Text="$([System.Text.RegularExpressions.Regex]::Replace( $(Prop), '_(\d*)\.', '_$1_' ) )" />
</Target>

If the use case is more complicated there are other options:

  • use another regular expression in the code above
  • MSBuild Community tasks has a Regex task which supports the Count parameter of Regex.Replace, so you can use a simple regex to replace "." with "_" and set count to one to only do the first instance
  • use an inline task in which you write your preferred implementation to replace the first instance from the answers here
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top