Search code examples
regexnant

Regex to match everything before a word. To be used in nant script


I was writing some build scripts for my project. I wanted a regex pattern which can match everything before a particular word. For eg: My script looks like this

Create Table ABC(
id int(50)
)

--//@UNDO

Drop table ABC

I want to match everything before --//@UNDO using nant regex task. How do I implement it??

I also want it to match everything in the file if --//@UNDO is not present in the file. I am not getting a way around


Solution

  • This is what I ended up doing

    <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>
    

    I found the index of --//@UNDO. And depending on its presence I am doing a choose when.. Solved the problem