如何使用NAnt更改文件中的一行?

Nel*_*ius 6 .net nant

我需要使用NAnt来更新.js文件中的一个特定行.该行将是这样的:

global.ServerPath = 'http://server-path/';
Run Code Online (Sandbox Code Playgroud)

我需要一种方法来更新该行的"服务器路径"部分与目标服务器的部分.
ReplaceString并不好,因为当我更新它时,我不知道文件中的路径是什么.

有帮助吗?

提前致谢

The*_*man 12

如果string::replace不起作用就<regex>可以完成这项工作.就是这个:

<?xml version="1.0" encoding="utf-8" ?>
<project name="replace.line" default="replace">
  <target name="replace" descripton="replaces a line">
    <property
      name="js.file"
      value="C:\foo.js" />
    <loadfile file="${js.file}" property="js.file.content" />
    <regex
      input="${js.file.content}"
      pattern="(?'BEFORE'.*)global\.ServerPath\s*=\s*'[^']*';(?'AFTER'.*)" />
    <echo
      file="${js.file}"
      message="${BEFORE}global.ServerPath = 'http://bla/';${AFTER}"
      append="false" />
  </target>
</project>
Run Code Online (Sandbox Code Playgroud)

  • 我和Aedna有同样的问题.使用`(?'BEFORE'[\ w\s\W]*)`而不是`(?'BEFORE'.*)`(和AFTER匹配组类似)对我有用.我想知道它是否是编码的东西. (2认同)

小智 5

为了能够捕获所有行,应该不是[\ w \ s \ W] *而不是AFTER之后的。*吗?

在我的情况下,*仅捕获行,而[\ w \ s \ W] *适用于整个文件