tags:

views:

141

answers:

2

I'm trying to to a trim to some values using replaceregexp. Everything looks great when I try it in software like EditPad Pro.

Here's a sample of what I want to accomplish:

mf.version.impl = 2.01.00  
mf.version.spec= 2.01.00  

Notice the extra spaces after the last digit.

Then I'm using this pattern:

[0-9]+.[0-9]+.[0-9]+[ ]*

But it doesn't work in Netbeans.

Here's my ant command for it:

<!--If postfix is empty, remove the empty space-->
    <replaceregexp file="../Xinco/nbproject/project.properties"
                   match="mf.version.spec?=?[0-9]+.[0-9]+.[0-9]+[ ]*"
                   replace="mf.version.spec = ${version_high}.${version_mid}.${version_low}"
                   byline="false"/>
    <replaceregexp file="../Xinco/nbproject/project.properties"
                   match="mf.version.impl?=?[0-9]+.[0-9]+.[0-9]+[ ]*"
                   replace="mf.version.impl = ${version_high}.${version_mid}.${version_low}"
                   byline="true"/>

${version_high}.${version_mid}.${version_low} are variables already defined that correspond to 2.01.00 respectively.

It results in

mf.version.impl = 2.01.00 
mf.version.spec = 2.01.00 

Notice one extra space after the last digit.

I did debug the ant calls and it seems like the above command is not executing like a match didn't occur.

Any idea?

+1  A: 

You should probably escape your .'s and use a capture group

e.g. (Perl regex for example)

s/([0-9]+\.[0-9]+\.[0-9]+)[ ]*/$1/
ternaryOperator
+1  A: 

Since you don't care about the value, you don't have to match it explicitly. Try:

^mf\.version\.impl\s*=.*$

Meaning:

  • ^ - start of the line (on multiline mode)
  • mf\.version\.impl - the string "mf.version.impl" literally, with the dots escaped.
  • \s* - zero or more spaces
  • .* - anything else (we can ignore the version, since you change it with a constant), all the way through to the...
  • $ - end of the line

Bonus track:

Looking at the specs, it looks like you can catch both lines with a single regex (not sure it works though):

^(mf\.version\.(impl|spec))\s*=.*$

and the replace rule:

replace="\1 = ${version_high}.${version_mid}.${version_low}"

This will replace \1 with the value it captured before, so again, you only need a single rule. (for trivia, usually $1 is used in replaces, but not here)

Kobi