Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式匹配单词前的所有内容。用于南音文字_Regex_Nant - Fatal编程技术网

Regex 正则表达式匹配单词前的所有内容。用于南音文字

Regex 正则表达式匹配单词前的所有内容。用于南音文字,regex,nant,Regex,Nant,我正在为我的项目编写一些构建脚本。我想要一个正则表达式模式,它可以匹配特定单词之前的所有内容。例如:我的脚本看起来像这样 Create Table ABC( id int(50) ) --//@UNDO Drop table ABC 我想在使用-/@UNDO之前匹配所有内容。我如何实现它 如果文件中不存在-/@UNDO,我还希望它匹配文件中的所有内容。我没有办法解决这个问题,这是一种模式: (?'str'.*?)--//@UNDO 结果将是str.如果您只想匹配字符串前的文本,而不是字符

我正在为我的项目编写一些构建脚本。我想要一个正则表达式模式,它可以匹配特定单词之前的所有内容。例如:我的脚本看起来像这样

Create Table ABC(
id int(50)
)

--//@UNDO

Drop table ABC
我想在使用-/@UNDO之前匹配所有内容。我如何实现它

如果文件中不存在-/@UNDO,我还希望它匹配文件中的所有内容。我没有办法解决这个问题,这是一种模式:

(?'str'.*?)--//@UNDO

结果将是str.

如果您只想匹配字符串前的文本,而不是字符串本身,则需要使用前瞻

.*?(?=--//@UNDO)

需要指定单线仍然适用。

这将是NAnt目标:

<target name="go">
  <loadfile
    file="C:\foo\bar.sql"
    property="content" />
  <regex
    pattern="(?'content'.*)--//@UNDO"
    input="${content}"
    options="Singleline"
    failonerror="false" />
  <echo message="${content}" />
</target>

请注意,如果文件中不存在-/@UNDO,则属性内容将与完整的文件内容一起预设。

这就是我最后要做的

<loadfile file="${filePathAndName}" property="file.contents" />
    <property name="fileHasUndo" value="${string::index-of(file.contents, '--//@UNDO')}" />
    <choose>
      <when test="${fileHasUndo ==  '-1' }">
          <echo file="${file}" append="true" message="${file.contents}" />
      </when>
      <otherwise>
        <regex pattern="(?'sql'[\s\S]*)--\/\/@UNDO[\s\S]*"  input="${file.contents}" options="Multiline"/>
        <echo file="${file}" append="true" message="${sql}" />
      </otherwise>
    </choose>

我找到了-/@UNDO的索引。根据它的存在,我在做一个选择。。已解决问题

您只需要正则表达式还是所有?看起来Nant允许您使用.Net正则表达式选项,您需要为其提供单线,即点匹配,以使上述正则表达式工作。@lxx否,我想您必须添加多行选项