Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild getting property substring before underscore symbol

Tags:

msbuild

In MSBuild I have a property which value is Name_Something. How can I get name part of this property.

like image 702
Sergej Andrejev Avatar asked Jun 22 '10 15:06

Sergej Andrejev


1 Answers

With MSBuild 4

If you use MSBuild 4, you could use the new and shiny property functions.

<PropertyGroup>
  <MyProperty>Name_Something</MyProperty>
</PropertyGroup>

<Target Name="SubString">
  <PropertyGroup>
    <PropertyName>$(MyProperty.Substring(0, $(MyProperty.IndexOf('_'))))</PropertyName>
  </PropertyGroup>

  <Message Text="PropertyName: $(PropertyName)"/>
</Target>

With MSBuild < 4

You could use the RegexReplace task of MSBuild Community Task

<PropertyGroup>
  <MyProperty>Name_Something</MyProperty>
</PropertyGroup>

<Target Name="RegexReplace">
  <RegexReplace Input="$(MyProperty)" Expression="_.*" Replacement="" Count="1">
    <Output ItemName ="PropertyNameRegex" TaskParameter="Output" />
  </RegexReplace>

  <Message Text="PropertyNameRegex: @(PropertyNameRegex)"/>
</Target>
like image 68
Julien Hoarau Avatar answered Nov 15 '22 01:11

Julien Hoarau