2012-04-30 44 views
1

基于this的线程。我试图在我的NAnt脚本中替换我的项目的当前版本。正则表达式分组结构未正确设置?

这里是我的替换片段

<target name="Replace"> 
    <loadfile file="${SetVersionCpp.File}" property="h.file.content" /> 
    <regex 
     input="${h.file.content}" 
     pattern="(?'BEFORE'[.\s]*)${LineBegining}\s*[\s\d,]*\r\n(?'AFTER'[.\s]*)" /> 
    <echo 
     file="${SetVersionCpp.File}" 
     message="${BEFORE}${LineBegining} ${ReplaceWith} 
     ${AFTER}" 
     append="false" 
     verbose="true" /> 
</target> 

我称之为这个文件

#define FILEVER 0, 2, 0, 3 
#define PRODUCTVER 0, 2, 0, 3 
#define STRFILEVER  "00.02.00.03\0" 
#define STRPRODUCTVER "00.02.00.03\0" 

以下参数

<property name="SetVersionCpp.File" value="${baseline.dir}\VersionNo.h" /> 
<property name="LineBegining" value="#define FILEVER" /> 
<property name="ReplaceWith" value="${FileVersion}" /> 

基于输出,变量后不捕捉由于某种原因,文件的其余部分。这就是我得到:

[#define FILEVER 0, 2, 0, 30 

] 

*我把它放在括号中,这样的空白得到了正确的格式

任何想法我做错了吗?

+0

我无法从你的代码段行尾是什么告诉在您的文件中使用,但\ r \ n不匹配unix行结尾。你确定你正在使用Windows风格的文本文件? – Nick

+0

我正在使用Wwindows。我只是尝试\ r和\ n而不是\ r \ n,并没有改变结果。 – petejamd

+0

如果我从'pattern'中删除类,'(?'BEFORE'。*)$ {LineBegining} \ s * [\ s \ d,] * \ r \ n(?'AFTER'。*) ',我得到结果中包含的第二行和不一致的行结尾 – petejamd

回答

1

我已经通过设置optionsSingleline,使.将匹配换行和从组中删除的类固定它:

<regex 
    input="${h.file.content}" 
    options="Singleline" 
    pattern="(?'BEFORE'.*)${LineBegining}\s*[\s\d,]*\r\n(?'AFTER'.*)" />