Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract number from string in MSBuild

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
like image 977
Ole Lynge Avatar asked Jun 05 '10 23:06

Ole Lynge


1 Answers

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>
like image 92
Julien Hoarau Avatar answered Sep 28 '22 01:09

Julien Hoarau