【发布时间】:2019-11-28 19:40:51
【问题描述】:
我想验证使用dotnet build /p:VERSION=1.2.3 和GeneratePackageOnBuild 打包一些nuget 包时使用的版本。我的正则表达式正在使用 C# 在 LINQPad 6 中工作:
Regex.Match("1.2.3", @"^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$")
但是,在我的 default.props 中,所有 csproj 文件(使用新的 sdk 样式,如果相关的话)正在导入它,但我有这个,但它根本不工作:
<Target Name="ValidateVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<VersionRegex>^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$</VersionRegex>
<VersionTest>1.2.3</VersionTest> <!-- Just to make it easier during testing -->
</PropertyGroup>
<Error
Text="Version is not following the correct format: $(VersionRegex)"
Condition=" $([System.Text.RegularExpressions.Regex]::IsMatch('$(VersionTest)', `$(VersionRegex)`)) " />
</Target>
如果我内联VersionRegex 和VersionTest,它仍然无法正常工作。任何想法为什么它在 C# 中工作而不是在 MSBuild 中?
【问题讨论】:
-
我在这个系统上一无所获(只是因为 C# 标签),但通常类型必须正确注释。而且那些看起来不像它们被正确地标记为字符串。下面的文本和条件字符串是如何注释的。
-
可能是因为 xml 转义没有达到预期的效果。尝试将您的正则表达式粘贴在 CDATA 中。
-
@viethoang:在 XML 中只有 5 个字符需要转义/编码:
<、>、&以及单引号(/撇号)和双引号.我没有看到任何这些 -
没错,接下来的想法是将 IsMatch() 中的四个单引号替换为 "
-
@Christopher 听起来很有道理。如何将它们注释为字符串?我尝试将变量内联为
[System.String]'^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$'并使用反引号,但我仍然得到相同的结果。
标签: c# regex .net-core msbuild csproj