Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regular expression substitution problem

807569Aug 16 2006 — edited Aug 17 2006
What I'm trying to do is a simple system property substitution for the properties file. The idea being that once the properties file has been loaded,
all the strings having form of ${system.property.key} will be substituted with the equivalent system property value for that given key.
For example if we have:
log.file = ${user.dir}\\logs\\system.log
at runtime this will become for example
log.file = c:\whatever\foobar\logs\system.log
I'm using regular expressions to perform the substitution. Everything is working
fine except the windows style paths. Somehow the regex strips all backslashes on windows paths.
Naturally on unix systems this is not a problem.

So with the above example the result would be
c:whateverfoobar\logs\system.log
The dummy test code is pretty simple
//		substitutes ${whatever} style patterns with system properties
		final Pattern p = Pattern.compile("\\$\\{([\\.\\w]+)\\}");
		Properties props = new Properties();
		
		try {
			props.load(new FileInputStream("config.properties"));
		
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
		Iterator it = props.entrySet().iterator();
		
		while (it.hasNext()) {
			StringBuffer sb = new StringBuffer();
			Map.Entry entry = (Map.Entry)it.next();
			Matcher m = p.matcher((String)entry.getValue());
			
			while (m.find()) {
				m.appendReplacement(sb, System.getProperty(m.group(1)));
			}
			m.appendTail(sb);		
			entry.setValue(sb.toString());
		}
Am I missing something here or what's the cause of the problem?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 14 2006
Added on Aug 16 2006
7 comments
233 views