regex - MSBuild 社区任务 RegexReplace 从路径中删除尾部斜杠

标签 regex msbuild msbuildcommunitytasks

我正在尝试使用 MSBuild 社区任务删除 OutputPath 末尾的斜杠

这就是我到目前为止所拥有的。

<RegexReplace Input="$(OutputPath)" Expression="\$" Replacement="" Count="1">
 <Output ItemName="FormattedOutputPath" TaskParameter="Output" />
</RegexReplace>
<Message Text="@(FormattedOutputPath)"/>

不幸的是,该消息仅返回我的路径,末尾仍带有斜杠。 路径是C:\MyDirectory\

看来我的表达不正确

有人可以帮忙吗?

最佳答案

斜杠用作转义字符,因此在模式中您必须用另一个斜杠转义斜杠字符:

<RegexReplace Input="$(OutputPath)" Expression="\\$" Replacement="" Count="1">
  <Output ItemName="FormattedOutputPath" TaskParameter="Output" />
</RegexReplace>
<Message Text="@(FormattedOutputPath)"/>

为了更好地理解转义,请参阅以下示例:

  1. $ 表示行/字符串结束
  2. \$ 表示美元符号字符
  3. \\ 表示斜杠字符
  4. \\$ 表示行/字符串末尾的斜杠字符

关于regex - MSBuild 社区任务 RegexReplace 从路径中删除尾部斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11247522/

相关文章:

regex - Perl:找不到文件时抑制反引号的输出

c# - 在自定义构建任务中运行 Project.Build() 时如何获取构建输出、错误列表?

c# - 通过命令提示符构建 VS 解决方案

MSBuild:为主项目生成 XML 文档,但不为依赖项目生成 XML 文档

msbuild - 如何使用 msbuild 下载文件?

visual-studio-2012 - MSBuildTasks 和 NuGet - 如何恢复 MSBuild.Community.Tasks.dll?

javascript - .net 正则表达式超过 5 个连续字母

regex - 在 Visual Studio Code 中搜索时匹配括号内的字符串

正则表达式和 sed 在多行 block 中查找字符串 block ,直到它以空格结尾

c# - FxCop 不尊重我的排除