This week's book giveaway is in the Testing forum.
We're giving away four copies of Practical Unit Testing with TestNG and Mockito and have Tomek Kaczanowski on-line!
See this thread for details.
The moose likes IDEs, Version Control and other tools and the fly likes Ant ReplaceRegExp Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Reply Bookmark "Ant ReplaceRegExp" Watch "Ant ReplaceRegExp" New topic
Author

Ant ReplaceRegExp

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm confused. This is getting into conflicts between XML and RegEx expressions and my fuddled brain which doesn't do either terriby well. I want an Ant task to replace a string in a Java source file:

I built the timestamp string, no problem. But the expression to find the old string is giving me fits. I tried

Of course, neither of those is what I want, because I want the quotes in the pattern. Looks like I need to get Regex to accept the spaces, and encode the quotes so that XML won't choke but regex will recognize. Any thoughts?
TIA


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
IIRC, both single and double quotes are allowed as delimiters in XML, so you could use 'VERSION = "(.*)"'.
Regarding the spaces, I am a little bit confused on what is the problem. But I guess you aren't interested in matching exactly one space character, anyway, so why don't you use 'VERSION\\s*=\\s*"(.*)"'? ('\\s*' stands for any number of white space.)
BTW, your dateformat should probably look "yyMMdd-hhmmss".


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ah, it gets more interesting. The error message comes about when the match WORKS. Here's my current attempt:

Something about the Ant task gets unhappy after finding the match when it attempts the substitution.
[ October 19, 2003: Message edited by: Stan James ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Ah - do you have backslashes in the datetime?
\x in the replace argument is referring to x-th group in the original pattern!
So
pattern "xy(.*)z" applied on "xyfooz" and replaced by "12\14" would result in "12foo4"! (\0 is referring the whole match).
If this is your problem, escape the backslashes by another one.
[ October 19, 2003: Message edited by: Ilja Preuss ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Interesting. My ${DateTime} value has only dashes like "2003-10-20-1659". I used it successfully in the filename of my archival zip file.
This works if I substitute in a simple string, like "arf". Wonder if there is something wrong in my use of the ${DateTime} macro?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That's strange. Perhaps you should get sure by putting the replace string into an echo task before the replaceregexp.
What regex engine are you using with ant?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What regex ... really don't know. I'm just using Ant as provided by Eclipse. I am running JDK 1.4.x but don't know if Eclipse or Ant know or care. I think I'll try using some different punctuation characters in my datetime string. Starting with none.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
From the Ant docu:

Ant will choose the regular-expression library based on the following algorithm:
If the system property ant.regexp.matcherimpl has been set, it is taken as the name of the class implementing org.apache.tools.ant.util.regexp.RegexpMatcher that should be used.
If it has not been set, first try the JDK 1.4 classes, then jakarta-ORO and finally try jakarta-regexp.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Hmm, with JDK 1.4.2

worked without any problems.
Notice, though, that your regex is greedy - it will replace anything from the first quote to the last one (not the next one)! If I remember correctly, you can change that by using ".*?".
 
 
subject: Ant ReplaceRegExp
 
Threads others viewed
remove control characters
regular expressions
String
regex : match any character ?
Compiling blank lines patterns using regex
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com