views:

178

answers:

2

I would like to extract the number from a string in MSBuild.

How can I do that using the built in tasks or the MSBuild.Community.Tasks? (RegexMatch might do, but how?)

Example: I have the string

agent0076

and I would like to get out the number, without the leading zeros:

76
A: 

The regex is very simple \d+. Once you have the string that matches that, you can use int.Parse to extract the value.

unholysampler
+1  A: 

Using MSBuild 4 Property Function

<Target Name="Regex">
  <PropertyGroup>
    <In>agent0076</In>
    <Out>$([System.Text.RegularExpressions.Regex]::Match($(In), `[1-9]\d*`))</Out>
  </PropertyGroup>

  <Message Text="Input : $(In) Output : $(Out)"/>
  <!-- Input : agent0076 Output : 76 -->
</Target>
madgnome
Yes, that works fine with MSBuild 4. But with MSBuild 3.5 I get the output : , '[1-9]\d*'))
Ole Lynge
Property function works only on MSBuild 4.
madgnome